等待线程的Java列表 [英] Java list of waiting threads
问题描述
有没有办法获取对象的等待线程列表/等待线程数?
Is there a way to get a list of waiting threads/number of waiting threads on an object?
推荐答案
如果你正在使用 synchronized
关键字 - 否。但是如果你使用 java.util.concurrent
锁,你可以。
If you are using the synchronized
keyword - no. But if you are using the java.util.concurrent
locks, you can.
ReentrantLock
有一个受保护的方法 getWaitingThreads()
。如果你扩展它,你可以公开它。
ReentrantLock
has a protected method getWaitingThreads()
. If you extend it, you can make it public.
更新:你正在使用 .wait()
和 .notify()
,因此您可以在wach 列表< Thread>
> .wait()调用 list.add(Thread.currentThread()
,并在每次通知之前删除它。这不是完美的,但实际上你不应该需要这样的清单。
Update: You are using .wait()
and .notify()
, so you can manually fill and empty a List<Thread>
- before wach .wait()
call list.add(Thread.currentThread()
, and remove it before each notify. It's not perfect, but actually you shouldn't need such a list.
这篇关于等待线程的Java列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!