Java Multithreading-(3) thread state conversion __java

Source: Internet
Author: User

One

The state transitions of threads are the basis of thread control. The total thread state can be divided into five states: Live, die, run, run, wait/block, respectively. Use a diagram to describe the following:



1, new state: The thread object has been created and no start () method has been invoked on it. 2. Operational status: When the thread is eligible to run, but the scheduler has not yet selected it as the state of the thread when it is running. When the start () method is invoked, the thread first enters the operational state.   After a thread has run or comes back from a blocking, waiting, or sleeping state, it is also returned to the operational state. 3. Running state: The state in which the thread scheduler selects a thread as the current thread from the runtime pool.   This is the only way in which a thread enters a running state. 4. Wait/block/Sleep status: This is the state in which the thread is eligible to run. In fact, this three-state combination is one of the common things: threads are still alive, but currently there is no condition to run.   In other words, it is operational, but if an event occurs, he may return to a running state. 5. Death state: When the thread's run () method is finished, it is considered dead. This thread object may be alive, but it is no longer a separate thread to execute. Once a thread dies, it cannot be resurrected. If the start () method is invoked on a dead thread, a Java.lang.IllegalThreadStateException exception is thrown.

Two

Threads under certain conditions, the state will change. The state transition diagram for a thread change is as follows:


1. New State (new): A newly created thread object. 2, Ready State (Runnable): After the thread object was created, other threads called the object's start () method. The thread of this state is in the running thread pool, becomes operational, and waits for access to the CPU. 3, running State (Running): The ready state of the thread to get the CPU, execute program code. 4, blocking State (Blocked): Blocking state is the thread for some reason to give up the use of CPU, temporarily stop running. Until the thread enters the ready state, there is an opportunity to go to the running state. The blocking situation is divided into three types: (i), wait for blocking: The running thread executes the wait () method, and the JVM puts the thread into the waiting pool. (ii), synchronous blocking: When a running thread acquires a synchronized lock on an object, the JVM puts the thread into the lock pool if the synchronization lock is occupied by another thread. (iii), other blocking: The running thread executes the sleep () or join () method, or when an I/O request is made, the JVM puts the thread into a blocking state. When the sleep () state times out, join () waits for the thread to terminate or timeout, or the I/O process completes, the thread is back in ready state. 5. State of Death (Dead): The thread has finished executing or exited the run () method because of an exception, which ends the lifecycle.

Related references:

http://lavasoft.blog.51cto.com/62575/27069

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.