多线程之继承Thread,调用start和run的区别
public class Thread1 extends Thread {
@Override
public void run() {
//run方法线程体
for (int i = 0;i < 20;i++){
System.out.println("run运行" + i);
}
}
public static void main(String[] args) {
//main线程,主线程
// 创建一个线程对象
Thread1 thread1 = new Thread1();
/**
* 调用run方法,先运行run方法,在运行main方法
* 调用start方法,main方法与run方法交替运行
*/
// thread1.run();
//调用start方法开启线程
thread1.start();
for (int i = 0;i < 1000;i++){
System.out.println("main运行" + i);
}
}
}
注意:线程开启不一定立即执行,由CPU调度执行

浙公网安备 33010602011771号