目录
1.调用run方法
2.调用start方法
3.start和run的区别
二、Thread中的start和run的区别
1.调用run方法
public class ThreadTest {
private static void attack() {
System.out.println("Current Thread is : " + Thread.currentThread().getName());
} public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(){
@Override
public void run() {
attack();
}
};
System.out.println("current main thread is : " + Thread.currentThread().getName());
t.run();
}
显示线程只有一个,即main线程
2.调用start方法
public class ThreadTest {
private static void attack() {
System.out.println("Current Thread is : " + Thread.currentThread().getName());
} public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> attack());
System.out.println("current main thread is : " + Thread.currentThread().getName());
t.start();
}
}
我们是用lambda表达式来重写的Thread类,这个时候就会创建一个新的线程