Five states of a thread

Source: Internet
Author: User

Threads are always in one of the following five states from creation, run to end: New state, ready state, running state, blocking state, and death status.

1. New Status

When a thread is created with the new operator. The program has not started running code in the thread at this point.

2. Ready State

A newly created thread does not automatically start running, and the thread's start () method must be called to execute the thread. When the thread object calls the start () method, the thread is started, the start () method creates the system resources that the thread runs, and the dispatch thread runs the run () method. When the start () method returns, the thread is in the ready state.

A thread that is in a ready state does not necessarily run the run () method immediately, and the thread must also compete with other threads for CPU time, only to get CPU time to run the thread. Because in a single CPU computer system, it is not possible to run multiple threads at the same time, only one thread is running at a time. Therefore, there may be multiple threads in the ready state at this time. Multiple threads that are in the ready state are dispatched by the thread scheduler of the Java Runtime System.

3. Operating status (running)

When the thread gets CPU time, it goes into a running state and really starts executing the run () method.

4. Blocking status (blocked)

During a thread run, it may enter a blocking state for various reasons:

① thread goes to sleep by calling the sleep method;

The ② thread invokes an operation that is blocked on I/O, that is, the operation does not return to its caller until the input-output operation is complete;

The ③ thread attempts to get a lock that is being held by another thread;

④ thread is waiting for a trigger condition;

The so-called blocking state is that the running thread is not running at the end, temporarily giving up the CPU, while other in the ready state of the thread can get CPU time, into the running state.

5. Death status (dead)

There are two causes of thread death:

①run method Normal exit and natural death;

② an uncaught exception terminates the Run method and causes a sudden death of the thread;

To determine whether a thread is currently alive (or is either operational or blocked), the IsAlive method is used, which returns true if it is operational or blocked, or false if the thread is still new and not operational, or if the thread is dead.

Five states of a thread

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.