The difference between calling start() and run()

1. Call the start() method:

Notifies the "thread planner" that the current thread is ready, waiting to call the run() method of the thread object. This process is to let the system arrange a time to call the run() method in Thread, so that the thread can be run, start the thread, and have the effect of asynchronous execution.

Call the start() method, which is the process of changing the thread state to a runnable state.

2. Call the run() method:

It is not executed asynchronously, but synchronously. The current thread is not handed over to the "thread planner" for processing, but the main thread to call the run() method, that is, it must wait for the code in the run() method to finish executing. The code behind can be executed.

 

Startup difference:

1  public  class MainTest {
 2  
3      public  static  void main(String[] args) {
 4          Thread thread= new ThreadDemo();  
 5          // The first  
 6          // Indicates: run() is no different from other method calls, The main method executes it in order, and prints out the last sentence  
 7          // thread.run();  
 8            
9          // The second  
 10          // Indicates: The start() method recreates a thread, after the execution of the main method ends , Since the thread created by the start() method does not end,  
 11          // So the main thread fails to exit until the thread thread is also executed. It should be noted here that the thread created by default is a user thread (non-daemon thread)  
 12          // thread .start();  
 13            
14         // The third type  
 15          // 1. Why didn't 100 sentences print out? Because we set the thread thread as a daemon (daemon) thread, when only the daemon thread exists in the program, it can exit, so only seven are printed. The sentence exits  
 16          // 2. When a daemon thread is running in the java virtual machine, the java virtual machine is closed. When all regular threads have finished running,  
 17          // No matter where the daemon thread runs, the virtual machine will exit. So your daemon thread is best not to write some business logic that will affect the program. Otherwise, it is impossible to predict what problems will occur in the program  
 18          // thread.setDaemon(true);  
 19          // thread.start();  
 20            
21          // The fourth type  
 22          // User threads can be forced to kill by System.exit(0) 
23  thread.start           ();  
 24          System.out.println("main thread is over" );  
 25          System.exit(1 );  
26         }  
27           
28     }
29     class ThreadDemo extends Thread{  
30     @Override  
31     public void run() {  
32         for (int i = 0; i < 100; i++) {  
33                 System.out.println("This is a Thread test"+i);  
34         }  
35     }  
36 
37 }
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324810619&siteId=291194637