我正在学习Android Studio中的工具,获取线程转储,如下所示:
我注意到像这样的每个线程的不同状态,
我可以看到有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)
答案 0 :(得分:5)
新表示如果您创建Thread
类的实例但在调用start()
方法之前线程处于新状态:
Thread t = new Thread(...); // t is New
Runnable 表示调用start()
方法后线程处于可运行状态。基本上是:
t.start(); // t is Runnable
运行是 Runnable 的“子状态”:如果线程调度程序选择了线程,则线程处于运行状态。您无法明确地执行任何操作,这意味着您调用start()
然后等待。
就绪是 Runnable 的另一个“子状态”:该线程有资格运行并等待线程调度程序选择它。
已阻止表示线程仍处于活动状态但当前无法运行的状态。例如,当一个线程遇到synchronized
块时,会发生这种情况,该块由另一个线程处理。在这种情况下,第一个帖子变为被阻止。
等待是您的帖子既不是被阻止也不是准备好的状态。当您在某个主题上调用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
还有一个名为定时等待的状态,这几乎是一样的,但是由调用sleep()
引起。如果您致电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
终止是处于run()
方法退出时处于已终止或处于停止状态的线程。
我想我已经涵盖了所有内容:)这是一张图片,可以帮助您更清楚地理解它:
正如JoxTraex所要求的,这是我在发布之前阅读的资料:
答案 1 :(得分:2)
localStorage
线程状态。线程可以处于以下状态之一:
public static enum Thread.State
extends Enum<Thread.State>
线程在给定时间点只能处于一种状态。这些状态是虚拟机状态,不反映任何操作系统线程状态。
请参阅oracle's api以获取更多信息。