Java list of waiting threads

前端 未结 3 1100
忘掉有多难
忘掉有多难 2021-02-15 12:43

Is there a way to get a list of waiting threads/number of waiting threads on an object?

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-15 12:57

    If you are using the synchronized keyword - no. But if you are using the java.util.concurrent locks, you can.

    ReentrantLock has a protected method getWaitingThreads(). If you extend it, you can make it public.

    Update: You are using .wait() and .notify(), so you can manually fill and empty a List - 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.

提交回复
热议问题