I have bunch of code & the processor is small,
when calling notifyAll, sometimes creating performance issue (100ms) so I want to know what are the threads currently waiting for the object to get released.
synchronized (obj){
//do some stuff..
obj.notifyall();
}
I want to print all the threads waiting for the object to get released before calling obj.notifyAll()
Are all the threads waiting on the resource for the same condition? If yes, then you can try replacing
obj.notify()instead ofobj.notifyAllthough it really isn’t recommended. AFAIK, there is no way of “retrieving” a list of all the threads waiting on a given object (though you can programatically get a thread-dump of the process and view the threads, but I’m sure that’s not what you had in mind). Even if there was, listing threads and then doing “something” with them would surely take more than the time taken tonotifyAll.Also, if the “processor is small”, try limiting the number of threads spawned since without a fair amount of “real” threads, creating too many threads is typically an overhead. That way,
notifyAllwouldn’t wake up a bunch of threads.Here is a small program which demonstrates dumping of thread state information with comments inlined: