Java Data Type Tutorial - Java Thread.holdsLock(Object obj)








Syntax

Thread.holdsLock(Object obj) has the following syntax.

public static boolean holdsLock(Object obj)

Example

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.