Qwertie
Qwertie

Reputation: 17186

How to find out whether another thread is wait()ing?

I notice there is no isWaiting() method. Is it possible to test whether any thread is blocked wait()ing on a specific object? Given a Thread object, is it possible to find which object, if any, that Thread is wait()ing on?

Upvotes: 1

Views: 3729

Answers (2)

Pragalathan  M
Pragalathan M

Reputation: 1781

The basic concurrent methods may not suffice your needs. You may want to try java.util.concurrent package. One of the example classes that can solve your problem is ReentrantLock. Here is the complete tutorial on threads.

Upvotes: 0

Thomas
Thomas

Reputation: 88707

Well, for the waiting state try thread.getState() == Thread.State.WAITING || thread.getState() == TIMED_WAITING.

As for the object the thread waits for, I'm not sure but I doubt you could get the object the thread is wating for without native code. The otherway round, i.e. getting the thread which holds a lock on a given object, also seems to be impossible with pure Java (see here: Programmatically determine which Java thread holds a lock).

Upvotes: 8

Related Questions