File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Count of waiting threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Count of waiting threads" Watch "Count of waiting threads" New topic

Count of waiting threads

Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 691
Is there a way to get a count of threads that are waiting on an object's monitor?
Nitesh Kant

Joined: Feb 25, 2007
Posts: 1638

I am not aware to get that when one uses synchronized block. However, if you are using ReentrantLock then getQueueLength() will give you what you want.

apigee, a better way to API!
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 691
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
I think there's no way to know ,from within a Java program,the size of the wait set (the set containing the threads waiting on the mutex) of an object's monitor when the synchronized block is used,the reason being the fact that the implementation of the synchronization logic,achieved through the synchronized keyword,is internal to the JVM. However it would have been good to have a native method on the java.lang.Object class that could return this wait set count.
[ September 04, 2008: Message edited by: Ajay Saxena ]
It is sorta covered in the JavaRanch Style Guide.
subject: Count of waiting threads
It's not a secret anymore!