Thread lifecycle and state transitions detailed

Source: Internet
Author: User

(1) New state-(2) Ready state-((4) blocking state)-(3) Running state-(5) Dead state

(1)New: After creating the thread object, the thread is in the new state, and it cannot run at this time, just like other Java objects, only the Java virtual machine allocates memory for it and does not show the dynamic characteristics of any threads;
(2)Runnable: After the thread object invokes the start () method, the thread enters the ready state (also known as the running state). In the ready state of the thread is in the runtime pool, at this time it only has the conditions to run, can get the right to use the CPU to start running, but also need to wait for system scheduling;
(3)runing: The thread in the ready state gets the CPU right to start executing the thread execution body in the run () method, and the thread is in a running state. When a thread starts, it cannot be running all the time (unless its thread execution body is short enough and instantaneous), and when the system is allocated, the system will exfoliate the CPU resources that the thread occupies and give other threads an opportunity to execute. Only threads in a ready state can be converted to a running state.
(4)Blocked: An executing thread, in some special circumstances, such as executing time-consuming input/output operations, discards the use of the CPU and enters a blocking state. After the thread has entered the blocked state, it cannot enter the queued queue. A thread can enter a ready state only if the reason for the reference is blocked and is eliminated.
-When a thread attempts to acquire a synchronized lock on an object, if the lock is held by another thread, the current thread enters the blocking state and must acquire the lock held by the other thread if it wants to enter the ready state from the blocking state.
-When a thread calls a blocking Io method, the thread enters the blocking state and must wait until the blocked Io method returns if it wants to enter the ready state.
-When a thread calls the Wait () method for an object, it also causes the thread to enter a blocking state, and the Notify () method wakes.
--called the thread's sleep (long Millis). Thread sleep time will automatically enter the blocking state.
-When one thread invokes the join () method of another thread, the current thread enters the blocking state. When the newly joined thread finishes running, it will end the blocking state and enter the ready state.
The thread can only enter the ready state from the blocking state, not directly into the running state, that is, the thread that ends the blocking needs to re-enter the runtime pool and wait for the system to dispatch.
(5)terminated: The thread's Run () method executes normally or the thread throws an Exception exception (error), and the thread enters the dead state. Once in the dead state, the thread will no longer have the qualification to run, nor can it be converted to another state.

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.