Thread中的start和run方法的区别
public class ThreadTest {
private static void attack(){
System.out.println("Fight");
System.out.println("Current Thread is : " + Thread.currentThread().getName());
}
public static void main(String[] args) {
Thread t = new Thread(){
public void run() {
attack();
}
};
System.out.println("current main thread is :" + Thread.currentThread().getName());
t.run();
}
}
结果:
current main thread is :main
Fight
Current Thread is : main
public class ThreadTest {
private static void attack(){
System.out.println("Fight");
System.out.println("Current Thread is : " + Thread.currentThread().getName());
}
public static void main(String[] args) {
Thread t = new Thread(){
public void run() {
attack();
}
};
System.out.println("current main thread is :" + Thread.currentThread().getName());
t.start();
}
}
结果:
current main thread is :main
Fight
Current Thread is : Thread-0
通过JVM源码发现
扩充JVM,并且传入名字
- 调用start()方法会创建一个新的子线程并启动
- run()方法只是Thread的一个普通方法的调用