Java 线程中的“阻塞?计数”和“等待计数”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7170235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:56:19  来源:igfitidea点击:

What does "blocked?Count" and "Waited Count" in a Java Thread mean?

javamultithreading

提问by billowstao

I use JConsole to watch a thread, it shows

我使用 JConsole 观看一个线程,它显示

name: Thread-6
state:BLOCKED  sun.misc.Launcher$AppClassLoader@19821f ,owner: Thread-3
blocked?Count:199,645  Waited Count: 2,610

199,645 and 2,610: Is that a bad thing?

199,645 和 2,610:这是坏事吗?

回答by Pierre-Luc Bertrand

If your application is not running meeting your requirements, based on these numbers that would be because there is a lot of lock contention. Waiting is that it waits for a notification (Object.wait()) but blocked means that it tries to acquire a lock and cannot because another thread holds it.

如果您的应用程序运行未满足您的要求,根据这些数字,这可能是因为存在大量锁争用。等待是它等待通知(Object.wait())但被阻塞意味着它试图获取锁但不能因为另一个线程持有它。

From http://geekexplains.blogspot.ca/2008/07/threadstate-in-java-blocked-vs-waiting.html

来自http://geekexplains.blogspot.ca/2008/07/threadstate-in-java-blocked-vs-waiting.html

Difference between BLOCKED state and WAITING / TIMED_WAITING states?

BLOCKED 状态和 WAITING / TIMED_WAITING 状态之间的区别?

When a thread calls Object.wait method, it releases all the acquired monitors and is put into WAITING (or TIMED_WAITING if we call the timeout versions of the wait method) state. Now when the thread is notified either by notify() or by notifyAll() call on the same object then the waiting state of the thread ends and the thread starts attempting to regain all the monitors which it had acquired at the time of wait call. At one time there may be several threads trying to regain (or maybe gain for the first time) their monitors. If more than one threads attempt to acquire the monitor of a particular object then only one thread (selected by the JVM scheduler) is granted the monitor and all other threads are put into BLOCKED state.

当线程调用 Object.wait 方法时,它会释放所有获取的监视器并进入 WAITING(或 TIMED_WAITING,如果我们调用 wait 方法的超时版本)状态。现在,当线程通过对同一对象的 notify() 或 notifyAll() 调用通知时,线程的等待状态结束,线程开始尝试重新获得在等待调用时获得的所有监视器。有一次可能有几个线程试图重新获得(或者可能是第一次获得)他们的显示器。如果多个线程尝试获取特定对象的监视器,则只有一个线程(由 JVM 调度程序选择)被授予监视器,而所有其他线程都被置于 BLOCKED 状态。