Novice Learning JAVA (11)-Multithreading----thread lifecycle __java

Source: Internet
Author: User

New and ready state running state and blocking state thread death

When a thread is created and started, it is not in the beginning and is not always running. Threads are also life-cycle, including: Create (New), Ready (Runnable), blocking (Blocked), running (Running), Death (Dead) five states. When the thread is running, it is not possible to always occupy the CPU running alone, but always in the running, ready to switch between the state.
new and Ready status

When a program creates a thread from new, the thread is in the new state, but at this point the thread does not represent any of the threads, and like other normal Java objects, the Java Virtual machine allocates memory for it and initializes the value of its variable.

When the thread object invokes the start () method, the thread enters the ready state, not the running state, the Java virtual machine creates the method call stack and the program counter for it, and the thread indicates that it can be run, but when it is run, it needs to wait for the JVM's dispatch.

There is a need to note that starting a thread needs to call the start () method, rather than run () to start the thread, if the direct call to the Run method, the system will take the thread as a normal object, run () method is only a common method. To give you a chestnut:

Package lifecycle;

public class Invokerun extends thread{
    private int i;

    public void Run () {The For
        (; i<100;i++) {
            System.out.println (Thread.CurrentThread (). GetName () + "" +i);
        }
    Public

    static void Main (string[] args) {for
        (int i=0;i<100;i++) {
            System.out.println ( Thread.CurrentThread (). GetName () + "" +i);
            if (i==20) {
                new Invokerun (). run ();
                New Invokerun (). Run ();}}}

When you start a thread with the run () method, the end result is that the entire program runs with only one thread. The program is programmed in a single thread. Primarily because, when you start a thread, if you invoke the start () method, the system treats the run () method as the executing body of the thread. If you call the run () method directly, the other threads cannot run concurrently until the run () method returns.

The start () method can only be called on a thread that is in the new state, otherwise a illegalthreadstateexception exception is thrown

run state and blocking state

When a ready thread gets the CPU, it enters the running state, and if the system has only one CPU, there will be multiple threads rotating on the same CPU, and a CPU is running at any time with only one thread. If there is more than one CPU, there will be multiple threads parallel (parallel) execution;
A thread cannot be running all the time, unless the thread is able to do it in a short enough period, otherwise the thread will be interrupted several times during execution, in order to give the other threads an opportunity to execute, with the specific need to consider the execution strategy behind the scenes.

The diagram above is a state transition diagram of the thread, we can see from the diagram above that the thread is under what conditions to enter into a blocking (Blocked) state, and under what conditions to enter the ready (Runnable) state; As you can see from the diagram above, the thread does not go directly from the running state to the ready state, In addition to the yield () method.

thread Death

The thread ends in the following three ways, and ends with a dead state. The run () or call () method completes, and the thread ends normally. The thread throws an exception or error that is not captured. Call the thread's Stop () method directly to end the thread--a method that usually leads to deadlocks and is not recommended for use.

When a thread dies, the other threads are not affected by it and end. After the other child threads start, its position and the main thread are equivalent, not affected by the main thread.

How to test whether a thread is dead or not. Measured by the IsAlive () method of the Thread object, when the thread is in a ready, running, blocking state, the result returned is true and False when the thread is in the dead, new state. Here's an example of calling the start () method on a dead thread:

Package startdead;

public class Startdead extends Thread {

    private int i;

    public void Run () {for
        (; i<100;i++) {
            System.out.println (getName () + "" +i);
        }
    /**
     * @param args */public
    static void Main (string[] args) {

        startdead sd =new startdead ();
        for (int i=0;i<300;i++) {
            System.out.println (Thread.CurrentThread (). GetName () + "" +i);
            if (i==20) {
                sd.start ();
                System.out.println (Sd.isalive ());
            }
            When the i>20, the thread must have been started, if Sd.isalive () is false, that is the state of death; if
            (i>20 &&!sd.isalive ()) {
                //attempt to start the thread again
                Sd.start ();}}}


Calling the start () method on an already dead thread throws a illegalthreadstateexception exception.

Exception in thread ' main ' java.lang.IllegalThreadStateException at
    Java.lang.Thread.start (thread.java:595) At
    StartDead.StartDead.main (startdead.java:27)

Do not attempt to call the start () method on a dead thread, death is death, and the thread of death will not be used again as the thread's execution body. The program can only invoke the start () method on a thread that is new, and it is also an error to call Start () two times for a thread that is in the new state, which can cause illegalthreadstateexception Exceptions

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.