gpt4 book ai didi

java - 线程转储显示不正确的线程状态

转载 作者:行者123 更新时间:2023-11-29 03:39:25 25 4
gpt4 key购买 nike

我正在尝试诊断生产问题。我在 Mac OS Lion 上设置了一个启动 10 个线程的小测试程序(使用 Executors.newCachedThreadPool(),它们都在同步块(synchronized block)中调用 MUTEX.wait())

然后我执行 kill -3 以获取线程转储,我看到我的所有线程都显示 BLOCKED。这些难道不应该等待吗?

代码是这样的,(请原谅为简洁而引入的代码味道)

ExecutorService executor = Executors.newCachedThreadPool();
final Object MUTEX = new Object();
for(int i = 0; i < 10; i++) {
executor.execute(new Runnable() {
public void run() {
synchronized(MUTEX) {
MUTEX.wait();
} } }}

此时所有线程都应该处于 WAITING 状态,但实际上线程转储显示所有线程都被阻塞

最佳答案

因为您的线程正在等待监视器锁以进入同步块(synchronized block)/方法,所以它们的状态是BLOCKED

阻塞等待监视器锁的线程处于 BLOCKED 状态,无限期等待另一个线程执行特定操作的线程处于 WAITING > 状态。

有关 BLOCKEDWAITING 之间区别的更多详细信息,请参见下文:

来自 BLOCKED 的 JavaDoc :

Thread state for a thread blocked waiting for a monitor lock. A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling Object.wait.

来自 WAITING 的 JavaDoc :

A thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate.

关于java - 线程转储显示不正确的线程状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13863426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com