Online Java Compiler By
JavaTpoint.com
public class JavaHoldLockExp implements Runnable { public void run() { // print currently executing thread System.out.println("Currently executing thread is: " + Thread.currentThread().getName()); // returns true if the current thread holds the lock on the specified object System.out.println("Does thread holds lock? " + Thread.holdsLock(this)); synchronized (this) { System.out.println("Does thread holds lock? " + Thread.holdsLock(this)); } } public static void main(String[] args) { JavaHoldLockExp g1 = new JavaHoldLockExp(); // create a thread Thread t1 = new Thread(g1); // this will call run() function t1.start(); } }
Output