Why can't the start method be called repeatedly? And the run method can?

Posted by Intelly XAD on Tue, 15 Feb 2022 04:16:20 +0100

When you first learn threads, you always confuse the run method and the start method. Although they are two completely different methods, they are difficult to distinguish when you first use them. The reason is that the effect seems to be the same when you first use them. The following code is shown:

public static void main(String[] args) {
    // Create thread 1
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("Execution thread 1");
        }
    });
    // Call the run method
    thread.run();

    // Create thread 2
    Thread thread2 = new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("Execution thread 2");
        }
    });
    // Call the start method
    thread2.start();
}

The results of the above procedures are as follows:

From the above results, it can be seen that the execution effect of both calls is the same, and both tasks can be executed successfully. However, if you print the name of the current thread when executing the thread, you can see the difference between the two, as shown in the following code:

public static void main(String[] args) {
    // Create thread 1
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            // Get current execution thread
            Thread currThread = Thread.currentThread();
            System.out.println("Executing thread 1, thread Name:" + currThread.getName());
        }
    });
    // Call the run method
    thread.run();

    // Create thread 2
    Thread thread2 = new Thread(new Runnable() {
        @Override
        public void run() {
            // Get current execution thread
            Thread currThread = Thread.currentThread();
            System.out.println("Executing thread 2, thread Name:" + currThread.getName());
        }
    });
    // Call the start method
    thread2.start();
}

The results of the above procedures are as follows:

From the above results, we can see that when calling the run method, it actually calls the current main program to execute the method body; Calling the start method is really creating a new thread to execute the task.

Difference 1

The first difference between the run method and the start method is that calling the start method is to really start a thread to execute a task, while calling the run method is equivalent to executing the ordinary method run and does not start a new thread, as shown in the following figure:

Difference 2

The second difference between the run method and the start method is that the run method is also called the thread body, which contains the specific business code to be executed. When the run method is called, the code in the run method will be executed immediately (if the current thread time slice is not used up); When the start method is called, a thread is started and the state of the thread is set to ready state. That is, calling the start method does not execute immediately.

Difference 3

Because the run method is an ordinary method, which can be called many times, the run method can be called many times; The start method is to create a new thread to execute the task, because the thread can only be created once, so their third difference is that the run method can be called multiple times, while the start method can only be called once.
The test code is as follows:

// Create thread 1
Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        // Gets the thread currently executing
        Thread currThread = Thread.currentThread();
        System.out.println("Executing thread 1, thread Name:" + currThread.getName());
    }
});
// Call the run method
thread.run();
// Call the run method multiple times
thread.run();

// Thread creation II
Thread thread2 = new Thread(new Runnable() {
    @Override
    public void run() {
        // Gets the thread currently executing
        Thread currThread = Thread.currentThread();
        System.out.println("Executing thread 2, thread Name:" + currThread.getName());
    }
});
// Call the start method
thread2.start();
// Call the start method multiple times
thread2.start();

The results of the above procedures are as follows:

From the above results, it can be seen that the run method can be called several times and can be executed normally. When the start method is called the second time, the program will report an error and prompt "IllegalThreadStateException" illegal thread state exception.

Why can't start be called repeatedly?

To find the answer to this question, you need to check the implementation source code of the start method. Its source code is as follows:

From the first line of the start source code implementation, we can get the answer to the question, because when the start method is executed, it will first judge whether the current thread state is equal to 0, that is, whether it is a NEW state NEW. If it is not equal to the NEW state, an illegal thread state exception of "IllegalThreadStateException" will be thrown, This is why the thread's start method cannot be called repeatedly.
Its execution process is: after the thread calls the first start method, the thread state will change from NEW to ready. At this time, call the start method again, and the JVM will judge that the current thread is not equal to the NEW state, thus throwing IllegalThreadStateException illegal thread state exception.

summary

The main differences between the run method and the start method are as follows:

  1. The nature of the method is different: run is an ordinary method, while start is a method to start a new thread.
  2. The execution speed is different: calling the run method will immediately execute the task, and calling the start method changes the state of the thread to the ready state, which will not be executed immediately.
  3. The number of calls is different: the run method can be called repeatedly, while the start method can only be called once.

The reason why the start method cannot be called repeatedly is that the state of the thread is irreversible. Thread has made a judgment in the implementation source code of start. If the thread is not in the NEW state, an illegal thread state exception IllegalThreadStateException will be thrown.

Right and wrong are judged by ourselves, bad reputation is heard by others, and the number of gains and losses is safe.

The official account: Java interview

Interview collection: https://gitee.com/mydb/interview

Topics: Java Back-end