class A implements Runnable {
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
class B implements Runnable {
public void run() {
new A().run();
new Thread(new A(), "name_thread2").run();
new Thread(new A(), "name_thread3").start();
}
}
public class Main {
public static void main(String[] args) {
new Thread(new B(), "name_thread1").start();
}
}
输出是什么?
============================
name_thread1
name_thread1
name_thread3
============================
new Thread(new A(), "name_thread2").run()和 new Thread(new A(), "name_thread3").start()不同的是 new Thread(new A(), "name_thread2").run()只是会在本线程内部执行,而 new Thread(new A(), "name_thread3").start()将会新建一个线程,然后在新建的线程中执行run,所以有以上结果。