java 线程的状态究竟意味着什么?
What does the java thread's state really mean?
我正在AndroidStudio学习工具,获取thread dump,如下:
我注意到像这样每个线程的不同状态,
我可以看到有 runnable
、sleeping
、waiting
。而我深入线程栈,大部分线程栈都是这样的,
"<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)
我很困惑他们都在 Object.wait
停止,但是线程的状态可以是 runnable
、sleeping
、waiting
?
这是其他状态线程的堆栈。
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)
public static enum Thread.State
extends Enum<Thread.State>
线程状态。线程可以处于以下状态之一:
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.
一个线程在给定的时间点只能处于一种状态。这些状态是不反映任何操作系统线程状态的虚拟机状态。
参考oracle's api了解更多信息。
New 表示如果您创建 Thread
class 的实例但在调用 start()
方法之前线程处于新状态:
Thread t = new Thread(...); // t is New
Runnable表示调用start()
方法后线程处于运行nable状态。基本上:
t.start(); // t is Runnable
运行是Runnable的"sub-state":线程在运行ning如果线程调度程序已经 select 编辑它。你不能明确地对它做任何事情,这意味着你调用 start()
然后等待。
Ready是Runnable的另一个"sub-state":线程有资格运行ning等待线程调度程序 select 它。
Blocked表示线程还活着时的状态,但是目前没有资格运行。例如,当一个线程遇到由另一个线程处理的 synchronized
块时,就会发生这种情况。在这种情况下,第一个线程变为 blocked.
Waiting 是您的线程既没有 blocked 也没有 ready 的状态。当您在线程上调用 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
还有一种叫Timed Waiting的状态,跟差不多,不过是调用sleep()
造成的。如果调用 wait(timeout)
或 join(timeout)
,线程也会进入 timed waiting 状态。
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 是在 run()
方法退出时处于终止或死状态的线程。
我想我已经涵盖了所有内容:)这是一张图片,可以帮助您更清楚地理解它:
应 JoxTraex 的要求,以下是我在发帖前阅读的来源:
- https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.html
- javarush.ru
- http://www.uml-diagrams.org/examples/java-6-thread-state-machine-diagram-example.html
- http://www.journaldev.com/1044/thread-life-cycle-in-java-thread-states-in-java
这只是谷歌搜索技巧的问题,真的...
我正在AndroidStudio学习工具,获取thread dump,如下:
我注意到像这样每个线程的不同状态,
我可以看到有 runnable
、sleeping
、waiting
。而我深入线程栈,大部分线程栈都是这样的,
"<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)
我很困惑他们都在 Object.wait
停止,但是线程的状态可以是 runnable
、sleeping
、waiting
?
这是其他状态线程的堆栈。
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)
public static enum Thread.State
extends Enum<Thread.State>
线程状态。线程可以处于以下状态之一:
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.
一个线程在给定的时间点只能处于一种状态。这些状态是不反映任何操作系统线程状态的虚拟机状态。
参考oracle's api了解更多信息。
New 表示如果您创建 Thread
class 的实例但在调用 start()
方法之前线程处于新状态:
Thread t = new Thread(...); // t is New
Runnable表示调用start()
方法后线程处于运行nable状态。基本上:
t.start(); // t is Runnable
运行是Runnable的"sub-state":线程在运行ning如果线程调度程序已经 select 编辑它。你不能明确地对它做任何事情,这意味着你调用 start()
然后等待。
Ready是Runnable的另一个"sub-state":线程有资格运行ning等待线程调度程序 select 它。
Blocked表示线程还活着时的状态,但是目前没有资格运行。例如,当一个线程遇到由另一个线程处理的 synchronized
块时,就会发生这种情况。在这种情况下,第一个线程变为 blocked.
Waiting 是您的线程既没有 blocked 也没有 ready 的状态。当您在线程上调用 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
还有一种叫Timed Waiting的状态,跟差不多,不过是调用sleep()
造成的。如果调用 wait(timeout)
或 join(timeout)
,线程也会进入 timed waiting 状态。
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 是在 run()
方法退出时处于终止或死状态的线程。
我想我已经涵盖了所有内容:)这是一张图片,可以帮助您更清楚地理解它:
应 JoxTraex 的要求,以下是我在发帖前阅读的来源:
- https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.html
- javarush.ru
- http://www.uml-diagrams.org/examples/java-6-thread-state-machine-diagram-example.html
- http://www.journaldev.com/1044/thread-life-cycle-in-java-thread-states-in-java
这只是谷歌搜索技巧的问题,真的...