Java examples for Thread:Thread Operation
Determining If the Current Thread Is Holding a Synchronized Lock
public class Main { public synchronized void myMethod() { boolean hasLock = false; Object o = new Object(); // Determine if current thread has lock for o hasLock = Thread.holdsLock(o); // false synchronized (o) { hasLock = Thread.holdsLock(o); // true }//w ww . j a va 2s . co m // Check if current thread has lock for current object hasLock = Thread.holdsLock(this); // true } }