Java - 如何知道线程何时等待? [英] Java - How to know when thread is waiting?
问题描述
是否有任何简洁的解决方案知道线程何时被放入等待
状态?我将线程放到等待
,并且我在需要时通知
。但有时我想知道线程当前是否正在等待,如果是,我必须做其他事情。
Is there any neat solution of knowing when a thread has been put into wait
status? I am putting threads to wait
and I notify
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.
我可能自己设置一个标志为true / false 。但我无法想象还有更好的方法吗?
I could probably set a flag myself to true/false. But I can't imagine there is a better way to do this?
推荐答案
方法 getState( )
一个线程返回一个 Thread.State
,它可以是:
The method getState()
of a thread returns a Thread.State
which can be:
NEW, RUNNABLE,BLOCKED,WAITING,TIMED_WAITING或TERMINATED
NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING or TERMINATED
参见 Thread.State 。
这篇关于Java - 如何知道线程何时等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!