“This is the 11th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

When you are learning threads, you will always confuse the run method with the start method. Although the two methods are completely different, it is hard to tell them apart when you first use them. The reason is that they seem to have the same effect when you first use them, 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(a) {
            System.out.println("Thread of execution one"); }});// Call the run method
    thread.run();

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

The execution results of the above procedures are as follows:As can be seen from the above results, the execution effect of both calls is the same, and the task can be successfully executed. However, we can see the difference if we print the name of the current thread when executing the thread, 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(a) {
            // Get the current thread of execution
            Thread currThread = Thread.currentThread();
            System.out.println("Execute 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(a) {
            // Get the current thread of execution
            Thread currThread = Thread.currentThread();
            System.out.println("Execute thread two, thread name:"+ currThread.getName()); }});// Call the start method
    thread2.start();
}
Copy the code

The execution results of the above procedures are as follows:From the above results, we can see:When the run method is called, the current main program is called to execute the method body; Calling the start method actually creates a new thread to execute the task.

The difference between 1

The first difference between the run method and the start method is that calling start actually starts a thread to execute the task, whereas calling run is equivalent to executing the normal run method and does not start a new thread, as shown in the figure below:

The difference between two

The second difference between the run method and the start method is that the run method is also called the thread body. It contains the specific business code to execute. When the run method is called, the code in the run method is executed immediately (if the current thread has not used up the time slot). The start method, on the other hand, starts a thread and sets its state to ready. That is, when the start method is called, it is not executed immediately.

The difference between 3

Because the run method is ordinary, and ordinary methods can be called multiple times, the run method can be called multiple times; The start method creates a new thread to execute the task, and since threads can only be created once, the third difference is that the run method can be called multiple times, whereas 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(a) {
        // Get the current thread of execution
        Thread currThread = Thread.currentThread();
        System.out.println("Execute thread 1, thread name:"+ currThread.getName()); }});// Call the run method
thread.run();
// Call the run method several times
thread.run();

// Create thread 2
Thread thread2 = new Thread(new Runnable() {
    @Override
    public void run(a) {
        // Get the current thread of execution
        Thread currThread = Thread.currentThread();
        System.out.println("Execute thread two, thread name:"+ currThread.getName()); }});// Call the start method
thread2.start();
// Call the start method several times
thread2.start();
Copy the code

The execution results of the above procedures are as follows:Can be seen from the above results, the run method calls can be used to perform many times, and invoke the start method for the second time when the program is an error, prompt “IllegalThreadStateException” illegal abnormal thread state.

Why can’t start be called repeatedly?

To find the answer to this question, look at the source code of the start method, which is as follows:From the start source code implementation of the first line, we can get the answer to the question, because the start method in the execution, will determine whether the current thread state is equal to 0, that is, whether the NEW state is NEW, if not equal to the NEW state, So will be thrown “IllegalThreadStateException” illegal thread state is unusual, this is the start method can not be repeated calls to the thread. Its execution process is:When a thread calls the first start method, its state changes from NEW to ready RUNNABLE. When it calls start again, the JVM determines that the current thread is no longer in the NEW state. Thus thrown IllegalThreadStateException illegal abnormal thread state.

conclusion

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

  1. Methods are different in nature: Run is a normal method, while start is a method to start a new thread.
  2. The execution speed is different: The run method executes the task immediately, while the start method changes the state of the thread to the ready state and does not execute the task immediately.
  3. The number of calls varies: the run method can be called repeatedly, whereas the start method can only be called once.

Start method is cannot be repeated calls to the reason for this is that the state of the Thread is not reversible, Thread made judge in the start the implementation of the source code, if the Thread is not a NEW state NEW, will throw illegal abnormal IllegalThreadStateException Thread state.

Judge right and wrong from yourself, praise to listen to others, gain and loss in the number.

Public number: Java interview analysis

Interview collection: gitee.com/mydb/interv…