启动线程
start()与run()的比较
public class StartAndRunMethod {
public static void main(String[] args) {
Runnable runnable = () ->{
System.out.println(Thread.currentThread().getName());
};
runnable.run(); //main
new Thread(runnable).start(); //Thread-2
}
}
start() 解读
不能重复start()
public class CantStartTwice {
public static void main(String[] args) {
Thread thread = new Thread();
thread.start();
thread.start();
}
}
源码解析:
-
启动新线程检查线程状态
-
加入线程组
-
调用start0()