Thread.holdsLock(Object obj) has the following syntax.
public static boolean holdsLock(Object obj)
In the following code shows how to use Thread.holdsLock(Object obj) method.
/*from w w w . ja v a 2 s. c om*/ class ThreadDemo implements Runnable { public void run() { System.out.println("Holds Lock = " + Thread.holdsLock(this)); synchronized (this) { System.out.println("Holds Lock = " + Thread.holdsLock(this)); } } } public class Main { public static void main(String[] args) { Thread th = new Thread(new ThreadDemo()); th.start(); } }
The code above generates the following result.