Java - How to know when thread is waiting?

2022-09-01 14:10:12

Is there any neat solution of knowing when a thread has been put into status? I am putting threads to and I them when i need it. But sometimes I want to know if a thread is currently waiting, and if so, I have to do something else.waitwaitnotify

I could probably set a flag myself to true/false. But I can't imagine there is a better way to do this?


答案 1

The method of a thread returns a which can be:getState()Thread.State

NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING or TERMINATED

See Thread.State.


答案 2

Have you looked at Thread.getState?