Thread, including what state? How is the transition between the states?

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/meism5/article/details/100549409

Thread of the life cycle

Thread including which state said the problem is that the life cycle of professional thread.
Different programming languages on the life cycle of the thread package is different.

 

Java threads in the life cycle

Java language threading a total of six state.

  1. NEW (initialization state)
  2. RUNNABLE (runs / run state)
  3. BLOCKED (blocked)
  4. WAITING (unlimited wait)
  5. TIMED_WAITING (waiting time is limited)
  6. TERMINATED (end state) at the operating system level, Java threads BLOCKED, WAITING, TIMED_WAITING is a state (hibernation). That is, as long as the Java threads in one of these three states, we will never have the right to use the CPU.

Figure: 

 

Change in the state of Java threads

1. NEW state to RUNNABLE

Java just created out of the Thread object is the NEW state, the operating system will not be scheduled for execution. NEW RUNNABLE transition from state to state calling thread object's start () method on it.

 

2. RUNNABLE BLOCKED state and transition

  • synchronized modification of the method, the code blocks in the same time allowing only one thread execution, other threads can only wait, wait for the thread to transition from RUNNABLE BLOCKED state.
  • While waiting for the thread to get synchronized implicit locks, it will change from BLOCKED to RUNNABLE state.
    In the operating system level, the thread will shift to the sleep state, but the JVM level, the state of Java threads will not change, that is, the state of Java threads will remain RUNNABLE state. JVM level is not concerned with the state of the operating system-related scheduling, because the JVM view, waiting for the right to use CPU (operating system level in an executable state) and wait for the I / O (operating system level is dormant) there is no difference, both in waiting for a resource, it is included in the RUNNABLE state.
    Java 在调用阻塞式 API 时,线程会阻塞,指的是操作系统线程的状态,并不是 Java 线程的状态。

 

3. RUNNABLE WAITING state and transition

  • Thread gets synchronized implicit locks, Calling an Object.wait () method, the status will change from RUNNABLE to WAITING; call Object.notify (), Object.notifyAll () method, the thread may transition from WAITING state to RUNNABLE.
  • Calling an Thread.join () method. join () is a thread synchronization method, if a thread object Thread t, when calling t.join () when the state of a thread will change from execution of the code RUNNABLE to the WAITING, to wait executing the thread t. When a thread t executed, the thread will wait for its transition from WAITING state to RUNNABLE state.
  • Call LockSupport.park () method state, the thread will change from RUNNABLE to WAITING; call LockSupport.unpark (Thread thread) can wake the target thread, the thread of the state of the target will shift from WAITING state to RUNNABLE.

 

4. RUNNABLE the state transitions TIMED_WAITING

  • Thread.sleep(long millis)
  • Object.wait(long timeout)
  • Thread.join(long millis)
  • LockSupport.parkNanos(Object blocker, long deadline)
  • LockSupport.parkUntil(long deadline)
    TIMED_WAITING 和 WAITING 状态的区别,仅仅是调用的是超时参数的方法。

 

5. RUNNABLE state to TERMINATED

  • After executing the thread run () method, it will automatically change to TERMINATED state
  • Execution run () method when an exception is thrown, it can also cause the thread to terminate
  • stop Thread class () method is no longer recommended

 


Collect and share the most valuable programming information 

Personal blog   |   Public Number   |   GitHub   |   code cloud 

 

Guess you like

Origin blog.csdn.net/meism5/article/details/100549409