Concurrent series "The difference between run() & start()"

The difference between execution run()and start()method:

public class MyThread extends Thread{
    
    
    public MyThread(){
    
    
      	System.out.println("MyThread构造方法:"+ Thread.currentThread().getName());
    }

    @Override
    public void run(){
    
    
      	System.out.println("run方法:"+ Thread.currentThread().getName());
    }
}
public class ThreadMain {
    
    
    public static void main(String[] args) {
    
    
      MyThread mythread = new MyThread();
      
      //看一下执行run()和start()的区别
      mythread.run(); 
      //mythread.start();
    }
}

run(): Execute the run()method immediately without starting a new thread, the execution result is:

MyThread构造方法:main
run方法:main

start(): The run()timing of the execution method is uncertain, a new thread is started, and the execution result is:

MyThread构造方法:main
run方法:Thread-0

Visible, MyThread.java class constructor is mainthread calls, while the run()method is being Thread-0called thread run()method is a method called automatically.

Guess you like

Origin blog.csdn.net/weixin_44471490/article/details/108936899