示例:
private static void attack(){
System.out.println("当前线程为: " +
Thread.currentThread().getName());
}
public static void main(String[] args) {
Thread t = new Thread(){
@Override
public void run(){
attack();
}
};
System.out.println("主线程为:" + Thread.currentThread().getName());
t.start();
t.run();
}
输出结果
主线程为:main
当前线程为: Thread-0
当前线程为: main
- 调用start()方法会创建一个新的子线程并启动
- run()方法只是Thread的一个普通方法的调用