写一个Thread类:
public class MyThread extends Thread {
public MyThread(){
super();
System.out.println("构造函数执行:" + Thread.currentThread().getName());
}
@Override
public void run() {
System.out.println("run方法执行:" + Thread.currentThread().getName());
}
}
然后调用这个类的run()和start()方法
public static void main(String[] args) {
MyThread myThread = new MyThread();
//myThread.start();
myThread.run();
}
构造函数执行:main
run方法执行:main
调用start()方法
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
//myThread.run();
}
构造函数执行:main
run方法执行:Thread-0
从这里可以看出,myThread的构造函数是被主方法调用的,run()方法只是自调用