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 (new):
When a thread is created with the new operator, such as new Thread (r), the thread has not started running, and the thread is in the new state. When a thread is in a new state, the program has not started running code in the thread

2. Ready state (Runnable)

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, thestart () 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 a ready state are dispatched by the thread scheduler (ThreadsScheduler) of the Java Runtime System.

3. Operating status (Running)

         when threads get cpu time, it goes into a running state, really starts executing run () method .

    4.  blocking status (Blocked)

         threads may enter a blocking state for various reasons during the run:
      The    1> thread goes to sleep by calling the sleep method;
        2> thread calls a i/ o The action that is blocked, that is, the operation does not return to its caller until the input-output operation is complete;
        3> thread tries to get a lock, The lock is being held by another thread, and the
        4> thread is waiting for a trigger condition;
         ......           

          the so-called The blocking state is that the running thread is not running at the end, giving up temporarily cpu, when other ready-to-state threads get cpu time, into the running state.

    5.  death Status (Dead)

There are two causes of thread death:
1) The Run method normally exits and dies naturally,
2) An uncaught exception terminates the Run method and causes the thread to sudden death.
In order to determine whether a thread is currently alive (that is, it is either operational or blocked), the IsAlive method needs to be used. This method returns true if it is operational or blocked, or false if the thread is still in the new state and is not operational, or 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.