The difference between execution
run()
andstart()
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 main
thread calls, while the run()
method is being Thread-0
called thread run()
method is a method called automatically.