public class Demo1 extends Thread
{
public static void main(String[] args)
{
Thread thread1 = new Demo2();
Thread thread2 = new Demo2();
System.out.println(thread1.getName());
System.out.println(thread2.getName());
thread1.run();
thread2.run();
}
}
public class Demo2 extends Thread{
public void run() {
System.out.println(getName());
while(true) {
}
}
}
运行结果:
Thread-0
Thread-1
Thread-0
(陷入死循环)
如果把main方法里面的
thread1.run();
thread2.run();
替换为
thread1.start();
thread2.start();
那么运行结果为
Thread-0
Thread-1
Thread-1
Thread-0
(陷入死循环)
new:只是开启了线程,但是如果不执行start,线程是没有开始执行,可以理解为开辟了一个空间,但是没有跑
start:线程的真正开始,可以理解为开始跑了
run:线程跑的时候执行的方法,注意,在没有start的时候,run就是个普通方法,所以thread1.run();thread2.run();的时候只执行了thread1.run();就进入了死循环,因为此时线程还未开启