区别start( )和run( )
public class MyThread extends Thread{
public MyThread(){
System.out.println("构造方法调用: "+Thread.currentThread().getName());
}
private int i=5;
@Override
public void run(){
System.out.println("run方法调用:"+Thread.currentThread().getName());
}
}
public class Run {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
构造方法调用: main
run方法调用:Thread-0
public class Run {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.run();
}
}
构造方法调用: main
run方法调用:main
- start( )—-启动线程,状态就绪,等待cpu的调用。构造方法被main线程调用,run被Thread-0调用
- run( )—- 只是普通的调用方法,没有另起线程。