The life cycle of threads in Java can be broadly divided into 5 states

Source: Internet
Author: User

The life cycle of threads in Java can be broadly divided into 5 states.

①new: This refers to the creation of an object of the thread class (or its subclasses) with the NEW keyword

②runnable: This situation refers to the object of the thread class calling the start () method, when the thread waits for the time slice to rotate to itself, in order to get the CPU, and the second is that the thread does not run its own run method when it is in the RUNNABLE state. When the time slice is exhausted, return to the runnable state, and there is a situation where the thread in blocked state returns to the Runnable state after the current blocked state has ended.

③running: The thread at this point is the runnable thread that gets the CPU, and the RUNNING state is the state that all the threads want to get.

④dead: A thread in the running state becomes a DEAD state after the Run method is executed.

⑤blocked: This state refers to a thread in the running state, for some reason, such as calling the Sleep method, waiting for user input, and giving up the current CPU to another thread.

The reason that a thread in the runnable state becomes blocked state, except that the thread calls the sleep method, waits for an input reason, and that the join method of another thread is called in the current thread, and that the method is locked when accessing an object's method.

Accordingly, when a thread in the blocked state goes to the runnable state by that state when the following conditions are met: The Sleep thread wakes (the time of sleep), obtains the user's input, ends the other thread that called the join, and obtains the object lock.

In general, both the thread in runnable and the thread in the running state switch to each other until the Run method is finished and the thread ends and enters the dead state.

The life cycle of threads in Java can be broadly divided into 5 states

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.