run()方法:在本线程内调用该Runnable对象的run()方法,可以重复多次调用;
start()方法:启动一个线程,调用该Runnable对象的run()方法,不能多次启动一个线程
public class Run_Start {
public static void main(String[] args) throws InterruptedException {
System.out.println("main thread start!");
Thread thread = new MyTestThread();
thread.run();//thread.start();
for(int i=0;i<20;i++)
{
System.out.println("main thread");
}
System.out.println("main thread end!");
}
}
class MyTestThread extends Thread
{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Thread new "+i);
}
}
}
1、run()和其他方法的调用没任何不同,main方法按顺序执行了它
main thread start!
Thread new 0
Thread new 1
Thread new 2
Thread new 3
Thread new 4
Thread new 5
......
Thread new 99
main thread
main thread
......
main thread end!
2、start()方法重新创建了一个线程,该线程的执行与主线程是并行的。在main方法执行结束后,由于start()方法创建的线程没有运行结束,因此主线程未能退出,直到线程thread也执行完毕.这里要注意,默认创建的线程是用户线程(非守护线程)
main thread start!
main thread
main thread
......
Thread new 0
Thread new 1
Thread new 2
Thread new 3
Thread new 4
Thread new 5
Thread new 6
Thread new 7
Thread new 8
Thread new 9
main thread end!
Thread new 10
Thread new 11
Thread new 12
Thread new 13
......
Thread new 99