Home » Java ReentrantLock hasQueuedThread() Method

Java ReentrantLock hasQueuedThread() Method

by Online Tutorials Library

Java ReentrantLock hasQueuedThread() Method

The hasQueuedThread(Thread thread) method of ReentrantLock class returns true if there is some thread is waiting for the this lock. Note that because cancellations may occur at any time, a true return does not guarantee that this thread will ever acquire this lock. This method is designed primarily to use in monitoring of the system state.

Syntax

Parameter

thread – the thread

Returns

true if the given thread is queued waiting for this lock

Throws

NullPointerException – if the thread is null.

Example 1

Test it Now

Output:

Thread-1 is Waiting to get the lock  Thread-1 has got the lock.   has queued Thread = false  Thread-1 has released the lock.  

Example 2

Test it Now

Output:

Thread-2 is Waiting to get the lock  Thread-3 is Waiting to get the lock  Thread-1 is Waiting to get the lock  Thread-2 has got the lock.   has queued Thread = false  Thread-2 has released the lock.  Thread-3 has got the lock.   has queued Thread = false  Thread-3 has released the lock.  Thread-1 has got the lock.   has queued Thread = false  Thread-1 has released the lock.  
Next TopicJava ReentrantLock

You may also like