Java VisualVM - 线程状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27406200/
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-08-11 04:18:48  来源:igfitidea点击:

VisualVM - Thread States

javajvmvisualvmjvisualvm

提问by Ali Shah Ahmed

Can someone please explain me the difference between Sleeping, Wait, Park, and Monitorthread states in VisualVM.

有人可以解释我的区别SleepingWaitPark,和Monitor在VisualVM的线程状态。

enter image description here

在此处输入图片说明

This is what I have found:

这是我发现的:

Running: thread is still running.
Sleeping: thread is sleeping (method yield() was called on the thread object)
Wait: thread was blocked by a mutex or a barrier, and is waiting for another thread to release the lock
Park: parked threads are suspended until they are given a permit. Unparking a thread is usually done by calling method unpark() on the thread object
Monitor: threads are waiting on a condition to become true to resume execution

Running: 线程仍在运行。
Sleeping:线程正在休眠(在线程对象上调用了方法 yield())
Wait:线程被互斥锁或屏障阻塞,正在等待另一个线程释放锁
Park:停放的线程将被挂起,直到获得许可。Unpark 线程通常是通过调用线程对象上的 unpark() 方法来完成的
Monitor:线程正在等待条件变为真以恢复执行

What I am unable to understand is the state Park, what actually suspends the thread? How do I detect in the code what has made the thread suspend its execution?

我无法理解的是州立公园,实际上是什么暂停了线程?我如何在代码中检测是什么导致线程暂停执行?

Can someone please guide me in this regard.

有人可以在这方面指导我。

Thanks.

谢谢。

采纳答案by Maciej Cygan

I have used google and the very first page that came up had a very nice diagram which pretty much describes all you need/want to know. Next time its worth trying google for these type of questions.

我用过谷歌,出现的第一页有一个非常漂亮的图表,它几乎描述了你需要/想知道的一切。下次值得尝试谷歌来解决这些类型的问题。

enter image description here

在此处输入图片说明

1) New

1) 新

The thread is in new state if you create an instance of Thread class but before the invocation of start() method.

如果在调用 start() 方法之前创建 Thread 类的实例,则线程处于新状态。

2) Runnable

2) 可运行

The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.

线程在调用 start() 方法后处于可运行状态,但线程调度程序没有选择它作为运行线程。

3) Running

3) 跑步

The thread is in running state if the thread scheduler has selected it.

如果线程调度程序选择了该线程,则该线程处于运行状态。

4) Timed waiting

4) 定时等待

Timed waiting is a thread state for a thread waiting with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:

定时等待是线程以指定的等待时间等待的线程状态。由于使用指定的正等待时间调用以下方法之一,线程处于定时等待状态:

  • Thread.sleep(sleeptime)
  • Object.wait(timeout)
  • Thread.join(timeout)
  • LockSupport.parkNanos(timeout)
  • LockSupport.parkUntil(timeout)
  • Thread.sleep(睡眠时间)
  • Object.wait(超时)
  • Thread.join(超时)
  • LockSupport.parkNanos(超时)
  • LockSupport.parkUntil(超时)

5) Non-Runnable (Blocked)

5) 不可运行(阻塞)

This is the state when the thread is still alive, but is currently not eligible to run.

这是线程还活着但当前没有资格运行时的状态。

6) Terminated

6) 终止

A thread is in terminated or dead state when its run() method exits.

线程在其 run() 方法退出时处于终止或死状态。

Hopefully this answers your question :).

希望这能回答你的问题:)。

Parking:

停车处:

Disables the current thread for thread scheduling purposes unless the permit is available.

除非许可可用,否则出于线程调度目的禁用当前线程。

Threads are being parked or suspended if you like to call it this way because it does not have a permission to execute. Once permission is granted the thread will be unparked and execute.

如果您喜欢以这种方式调用线程,则线程将被停放或挂起,因为它没有执行权限。一旦授予权限,线程将被解除停放并执行。

Permits of LockSupport are associated with threads (i.e. permit is given to a particular thread) and doesn't accumulate (i.e. there can be only one permit per thread, when thread consumes the permit, it disappears).

LockSupport 的许可与线程相关联(即许可被授予特定线程)并且不会累积(即每个线程只能有一个许可,当线程消耗该许可时,它就会消失)。

回答by Jeremy

VisualVM maps the Java thread state (as described in @Maciej's answer) to the state presented in its UI as follows:

VisualVM 将 Java 线程状态(如@Maciej 的回答中所述)映射到其 UI 中显示的状态,如下所示:

BLOCKED -> Monitor
RUNNABLE -> Running
WAITING/TIMED_WAITING -> Sleeping/Park/Wait (see below)
TERMINATED/NEW -> Zombie

Sleepingand Parkare specific cases of (timed) waiting:

Sleeping并且Park是(定时)等待的具体情况:

Sleeping: specifically waiting in Thread.sleep().  
Park:     specifically waiting in sun.misc.Unsafe.park() (presumably via LockSupport).

(The mapping is performed in ThreadMXBeanDataManager.java.)

(映射在 中执行ThreadMXBeanDataManager.java。)

A brief (and non-authoritative) discussion of Java thread state can be found here.

可以在此处找到有关 Java 线程状态的简短(非权威)讨论。

EDITED TO ADD:

编辑添加:

It's also worth noting that threads blocking in calls to native methods appear in the JVM as RUNNABLE, and hence are reported by VisualVM as Running(and as consuming 100% CPU).

还值得注意的是,在调用本机方法时阻塞的线程在 JVM 中显示为RUNNABLE,因此被 VisualVM 报告为Running(并且消耗 100% CPU)。