简体   繁体   English

java线程的状态究竟意味着什么?

[英]What does the java thread's state really mean?

I am learning the tool in Android Studio, get thread dump, as follow: 我正在学习Android Studio中的工具,获取线程转储,如下所示:

获取线程转储

I notice the different state of every thread like this, 我注意到这样的每个线程的不同状态,

在此输入图像描述

I can see there are runnable , sleeping , waiting . 我可以看到有runnablesleepingwaiting And I deep into the thread stack, most thread stack like this, 而且我深入到线程堆栈,大多数线程堆栈都是这样的,

"<61> RxComputationScheduler-3@830064517520" daemon prio=5 waiting
    java.lang.Thread.State: WAITING
        at java.lang.Object.wait(Object.java:-1)
        at java.lang.Thread.parkFor(Thread.java:1205)
        at sun.misc.Unsafe.park(Unsafe.java:325)
        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:157)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2017)
        at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1050)
        at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:778)
        at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1035)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1097)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)

I am confused that they do all halt at Object.wait , however the state of thread can be runnable , sleeping , waiting ? 我很困惑,他们都在Object.wait停止,但线程的状态可以runnablesleepingwaiting

Here is the other state thread's stack. 这是另一个状态线程的堆栈。

RUNNABLE

<53> RxSchedulerPurge-1@830057651944" daemon prio=5 runnable
  java.lang.Thread.State: RUNNABLE
      at java.lang.Object.wait(Object.java:-1)
      at java.lang.Thread.parkFor(Thread.java:1205)
      at sun.misc.Unsafe.park(Unsafe.java:325)
      at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:197)
      at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2056)
      at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1062)
      at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:778)
      at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1035)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1097)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
      at java.lang.Thread.run(Thread.java:841)</code>

TIMED_WAITING

<58> RxScheduledExecutorPool-2@830064740632" daemon prio=5 sleeping
  java.lang.Thread.State: TIMED_WAITING
      at java.lang.Object.wait(Object.java:-1)
      at java.lang.Thread.parkFor(Thread.java:1205)
      at sun.misc.Unsafe.park(Unsafe.java:325)
      at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:197)
      at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2056)
      at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1062)
      at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:778)
      at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1035)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1097)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
      at java.lang.Thread.run(Thread.java:841)

New means the thread is in new state if you create an instance of Thread class but before the invocation of start() method: 新的手段线程处于新的状态,如果你创建的实例Thread类,但的调用之前start()方法:

Thread t = new Thread(...); // t is New

Runnable means the thread is in runnable state after invocation of start() method. Runnable表示调用start()方法后线程处于runnable状态。 Basically: 基本上:

t.start(); // t is Runnable

Running is the "sub-state" of Runnable : the thread is in running state if the thread scheduler has selected it. RunningRunnable的“子状态”:如果线程调度程序选择了线程,则线程处于运行状态。 You can't do anything with it explicitly, meaning you call start() and then just wait. 你不能明确地做任何事情,这意味着你调用start()然后等待。

Ready is another "sub-state" of Runnable : the thread is eligible for running and waits for the thread scheduler to select it. ReadyRunnable的另一个“子状态”:该线程有资格运行并等待线程调度程序选择它。

Blocked means the state when the thread is still alive, but is currently not eligible to run. 阻止表示线程仍处于活动状态但当前无法运行的状态。 This happens, for example, when one thread comes across a synchronized block, which is processed by another thread. 例如,当一个线程遇到由另一个线程处理的synchronized块时,就会发生这种情况。 In this case the first thread becomes blocked . 在这种情况下,第一个线程被阻止

Waiting is the state when your thread is neither blocked nor ready . 等待是你的线程既没有被阻止也没有准备就绪的状态 This usually happens when you call wait() or join() on a thread. 当您在线程上调用wait()join()时,通常会发生这种情况。

Thread t1 = new Thread(); // t1 is New
Thread t2 = new Thread(); // t2 is New
t1.start(); // t1 becomes Runnable
t2.start(); // t2 becomes Runnable
t1.join(); // t2 becomes Waiting, because t1 is processed until it terminates

There is also a state called Timed Waiting , which is almost the same thing, but is caused by calling sleep() . 还有一个名为Timed Waiting的状态,它几乎是一样的,但是由调用sleep()引起。 If you call wait(timeout) or join(timeout) , the thread also gets timed waiting state. 如果您调用wait(timeout)join(timeout) ,则线程也会获得定时等待状态。

Thread t = new Thread(); // t is New
t.start(); // t is Runnable
t.sleep(4000); // t get state of Timed Waiting for 4 seconds

Terminated is a thread in terminated or dead state when it's run() method exits. run()方法退出时, Terminated是一个处于终止或死状态的线程。

I think I covered it all :) Here is an image to help you understand it more clearly: 我想我已经涵盖了所有:)这是一个图像,以帮助您更清楚地理解它:

线程生命周期Java

As JoxTraex requested, here're sources I read before posting: 正如JoxTraex所要求的,这里是我在发布之前阅读的资料:

  1. https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.html https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.html
  2. javarush.ru javarush.ru
  3. http://www.uml-diagrams.org/examples/java-6-thread-state-machine-diagram-example.html http://www.uml-diagrams.org/examples/java-6-thread-state-machine-diagram-example.html
  4. http://www.journaldev.com/1044/thread-life-cycle-in-java-thread-states-in-java http://www.journaldev.com/1044/thread-life-cycle-in-java-thread-states-in-java

It's just a matter of good googling skill, really... 这只是一个很好的谷歌搜索技巧,真的......

public static enum Thread.State
extends Enum<Thread.State>

A thread state. 线程状态。 A thread can be in one of the following states: 线程可以处于以下状态之一:

NEW
A thread that has not yet started is in this state.

RUNNABLE
A thread executing in the Java virtual machine is in this state.

BLOCKED
A thread that is blocked waiting for a monitor lock is in this state.

WAITING
A thread that is waiting indefinitely for another thread to perform a particular action is in this state.

TIMED_WAITING
A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.

TERMINATED
A thread that has exited is in this state.

A thread can be in only one state at a given point in time. 线程在给定时间点只能处于一种状态。 These states are virtual machine states which do not reflect any operating system thread states. 这些状态是虚拟机状态,不反映任何操作系统线程状态。

refer oracle's api for more information. 有关更多信息,请参阅oracle的api

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM