What does java thread state mean?

I am learning a tool in Android Studio, getting a stream dump, as shown below:

get thread dump

I notice a different state of each thread, like this,

enter image description here

I see that there is runnable, sleeping, waiting. And I'm deep in the thread stack, most thread threads are 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'm confused, they all stop at Object.wait, but the state of the stream can be runnable, sleeping, waiting?

Here is another stack of state fortunes.

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)
+4
source share
2 answers

, , Thread, start():

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

Runnable , 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() .

, :) , :

Java Life Cycle Life Cycle

JoxTraex, , :

googling, ...

+4
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 api .

+2

Source: https://habr.com/ru/post/1658883/


All Articles