java线程的状态究竟意味着什么?
作者:互联网
我正在学习Android Studio中的工具,获取线程转储,如下所示:
我注意到这样的每个线程的不同状态,
我可以看到有可运行,睡觉,等待.而且我深入到线程堆栈,大多数线程堆栈都是这样的,
"<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
<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)
解决方法:
如果您创建Thread类的实例但在调用start()方法之前,则表示线程处于新状态:
Thread t = new Thread(...); // t is New
Runnable表示调用start()方法后线程处于runnable状态.基本上:
t.start(); // t is Runnable
Running是Runnable的“子状态”:如果线程调度程序选择了线程,则线程处于运行状态.你不能明确地做任何事情,这意味着你调用start()然后等待.
Ready是Runnable的另一个“子状态”:该线程有资格运行并等待线程调度程序选择它.
阻止表示线程仍处于活动状态但当前无法运行的状态.例如,当一个线程遇到由另一个线程处理的同步块时,就会发生这种情况.在这种情况下,第一个线程被阻止.
等待是你的线程既没有被阻止也没有准备就绪的状态.当您在线程上调用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(超时)或join(超时),则线程也会获得定时等待状态.
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()方法退出时,Terminated是一个处于终止或死状态的线程.
我想我已经涵盖了所有:)这是一个图像,以帮助您更清楚地理解它:
正如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
这只是一个很好的谷歌搜索技巧,真的……
标签:java,android,performance,thread-dump,thread-state 来源: https://codeday.me/bug/20190527/1165458.html