I am learning the tool in Android Studio, get thread dump, as follow:
I notice the different state of every thread like this,
I can see there are runnable,sleeping,waiting. 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?
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
Threadclass but before the invocation ofstart()method:Runnable means the thread is in runnable state after invocation of
start()method. Basically:Running is the "sub-state" of Runnable: the thread is in running state if the thread scheduler has selected it. You can't do anything with it explicitly, meaning you call
start()and then just wait.Ready is another "sub-state" of Runnable: the thread is eligible for running and waits for the thread scheduler to select it.
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
synchronizedblock, which is processed by another thread. 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()orjoin()on a thread.There is also a state called Timed Waiting, which is almost the same thing, but is caused by calling
sleep(). If you callwait(timeout)orjoin(timeout), the thread also gets timed waiting state.Terminated is a thread in terminated or dead state when it's
run()method exits.I think I covered it all :) Here is an image to help you understand it more clearly:
As JoxTraex requested, here're sources I read before posting:
It's just a matter of good googling skill, really...