Java thread state introduced

Original: Reprinted indicate the original address https://www.cnblogs.com/fanerwei222/p/11867086.html

 

Java threads States Description:

Java official document of the state of Java threads to do some explanation, as follows;

public static enum Thread.State
extends Enum<Thread.State>

A thread state. A thread can be in one of the following states:

  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.

A thread can be in only one state at a given point in time. These states are virtual machine states which do not reflect any operating system thread states.

The document to be a little explanation:

Java threads A total of six states:

New (NEW) 
run (RUNNABLE) 
blocked (BLOCKED) 
waiting (WAITING) 
Timeout waiting (TIMED_WAITING) 
termination (TERMINATED)

Each state generates timing and presentation:

1. Create (NEW)

new new the Thread () -> New


2. run (RUNNABLE)

  2.1 Ready 
        Thread.start () -> start thread -> arrived at the gates of the warehouse run queue ready 
        Object.notify () -> from standby / timeout wait state ready queue back to 
        the Object.notifyAll () -> from wait / timeout wait state to the ready queue 
        LockSupport.unpark (the Thread) -> from standby / return to the ready queue timeout wait state 
        to Thread.yield () -> from the operating state back to the ready queue 
        acquires the synchronized (Object) block / object lock method -> from blocking back ready queue

     2.2 running 
        ready threads waiting in the dispatch system (get CPU) after successfully -> start run -> run (occupied CPU, holding the lock)


3. blocked (BLOCKED)

Waiting to acquire synchronized when the object lock (Object) block / method -> blocked 
the Thread.sleep ( Long timeout) -> from the blocked state into the operating state (release the CPU, holding the lock)


4. Wait (the WAITING)

Object.wait () -> enters the waiting state from an operating state (release the CPU, releases the lock) 
the Thread.join () -> enters a wait state from a running state 
LockSupport.park () -> enters the waiting state from a running state


The time-out period (TIMED_WAITING)

The Object.wait ( Long timeout) -> enter timeout wait state from running state (release CPU, the lock is released) 
Thread.sleep ( Long timeout) -> enter timeout wait state from running state (release CPU, holding the lock) 
the Thread. the Join ( Long timeout) -> enter the timeout wait state from a running state 
LockSupport.parkNanos ( Long timeout) -> enter the timeout wait state from a running state 
LockSupport.parkUntil ( Long timeout) -> from the operating state into the standby state timeout

 

6. Termination (TERMINATED)

Thread.run () after finished -> terminated


Thread interrupts Description: interrupt ()

Thread is waiting for state or blocking state, calling the thread's interrupt () method, will have the following three processes:
         1. interrupt flag will immediately be marked as true ;
         2. Because it is then blocked, interrupting mark true is cleared - -> so, it is a false;
         3. finally throws InterruptedException exception.

Guess you like

Origin www.cnblogs.com/fanerwei222/p/11867086.html