11.3-Full stack Java Note: Thread life cycle

Source: Internet
Author: User

a thread object has to undergo 5 states during its life cycle .

New status (New)

When a thread object is created with the new keyword, the thread object is in a new state. The newly-emerging thread has its own memory space, which is entered in a ready state by calling the start method.

Ready state (Runnable)

A thread in the ready state already has a running condition, but has not yet been assigned to the CPU and is in a "thread-ready queue" waiting for the system to allocate its CPU. The ready state is not an execution state, and when the system selects a thread object waiting to be executed, it goes into the execution state. Once the CPU is acquired, the thread goes into a running state and automatically calls its own Run method. There are 4 reasons why a thread is ready to enter the state:

1. New Thread, call Start () method, enter Ready state

2. Block the thread, block it, and enter the ready state.

3. Run the thread, call the yield () method, and go directly to the ready state.

4. Running the thread, the JVM switches the CPU resources from this thread to the other threads.

Operational status (Running)

The thread in the running state executes the code in its own run method until the other method is called to terminate, or wait for a resource to block or complete the task and die. If there is no execution end within a given time slice, it will be replaced by the system to return to the ready state. It is also possible to enter a blocking state due to some " events that cause blocking ."

Blocking state (Blocked)

blocking refers to pausing the execution of a thread to wait for a condition to occur (such as a resource is ready).

4

1. execute sleep (int millsecond)

2. execute wait ()

3. Io stream operation (read ()/write ()

4.join () thread Union : Use the Join method when a thread waits for another thread to finish before it resumes execution . Causes the thread that called the method to execute before this is done, that is, the thread that is waiting to invoke the method finishes execution and then proceeds down.

Death Status (teminiated)

death state is the thread life cycle The last stage. There are two causes of thread death. One is the normal running thread that finished it run () All work of the method;   or Destroy method has been Jdk

When a thread enters a dead state, it cannot go back to another state.



"Full stack Java notes" is a can help you from Zeto long for the full stack of Java Engineer Series of notes. The author is called Mr. G,10 years Java research and development experience, has been in the digital, Aerospace Institute of a Research and development center engaged in software design and research and development work, from small white gradually achieve engineers, senior engineers, architects. Proficient in Java Platform Software development, Master Java EE, familiar with various popular development framework.


The notes contain six parts from shallow into deep:

A-java Introductory Phase

B-Database from beginner to proficient

C-hand Edge mobile front end and web front end

D-j2ee from understanding to combat

E-java Advanced Frame Fine Solution

F-linux and Hadoop



This article is from the "12931675" blog, please be sure to keep this source http://12941675.blog.51cto.com/12931675/1945759

11.3-Full stack Java Note: Thread life cycle

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.