• notice
  • Congratulations on the launch of the Sought Tech site

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

When learning threading, I always confuse the run method and the start method. Although the two are completely different methods, it is difficult to distinguish them when they are first used. The reason is that the effect seems to be the same when they are used for the first time. Show:


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

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

The execution results of the above programs are as follows:
image.png
From the above results, it can be seen that the execution effect of the two calls is the same, and the tasks can be successfully executed. 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 one
     Thread thread = new Thread(new Runnable() {
         @Override
         public void run() {
             // Get the current thread of execution
             Thread currThread = Thread.currentThread();
             System.out.println("Execution thread one, thread name: " + currThread.getName());
         }
     });
     // call the run method
     thread.run();

     // create thread two
     Thread thread2 = new Thread(new Runnable() {
         @Override
         public void run() {
             // Get the current thread of execution
             Thread currThread = Thread.currentThread();
             System.out.println("Execution thread two, thread name: " + currThread.getName());
         }
     });
     // call the start method
     thread2.start();
}

The execution results of the above program are as follows:
image.png
From the above results, we can see that when the run method is called, the current main program main is actually called to execute the method body; and the start method is called to actually create 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 actually starts a thread to perform tasks, 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:
image.png

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) not used up); and when the start method is called, a thread is started and the thread's state is set to the ready state. That is to say, calling the start method will not execute immediately.

Difference 3

Because the run method is an ordinary method, and ordinary methods can be called multiple times, the run method can be called multiple times; and the start method is to create new threads to perform tasks, because threads can only be created once, so their first The three differences are: 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 one
Thread thread = new Thread(new Runnable() {
     @Override
     public void run() {
         // Get the currently executing thread
         Thread currThread = Thread.currentThread();
         System.out.println("Execution thread one, thread name: " + currThread.getName());
     }
});
// call the run method
thread.run();
// call the run method multiple times
thread.run();

// create thread two
Thread thread2 = new Thread(new Runnable() {
     @Override
     public void run() {
         // Get the currently executing thread
         Thread currThread = Thread.currentThread();
         System.out.println("Execution thread two, thread name: " + currThread.getName());
     }
});
// call the start method
thread2.start();
// call the start method multiple times
thread2.start();

The execution results of the above program are as follows:
image.png
From the above results, it can be seen that the run method can be executed normally if the run method is called multiple times, but the program reports an error when the start method is called for the second time, prompting "IllegalThreadStateException" illegal thread state exception.

Why can't start be called repeatedly?

To find the answer to this question, we need to look at the source code of the start method. Its source code is as follows:
image.png
From the first line of the start source code implementation, we can get the answer to the question, because the start method will first judge the current thread when it is executed. Is the state of 0 equal to 0, that is, whether it is a new state NEW, if it is not equal to the new state, then an "IllegalThreadStateException" illegal thread state exception will be thrown, which is why the thread's start method cannot be called repeatedly.
Its execution process is: when the thread calls the first start method, the state of the thread will change from the new state NEW to the ready state RUNNABLE. At this time, when the start method is called again, the JVM will determine that the current thread is no longer available. Equal to the new state, thus throwing IllegalThreadStateException illegal thread state exception.

Summarize

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

  1. The 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: calling the run method will execute the task immediately, and calling the start method will change 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 makes a judgment in the implementation source code of start. If the thread is not in the new state NEW, an illegal thread state exception IllegalThreadStateException will be thrown.


Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

lipitor online buy & lt;a href="https://lipiws.top/"& gt;atorvastatin 80mg over the counter& lt;/a& gt; oral atorvastatin

Xeagdo

2024-03-07

Leave a Reply

+