Loading

启动线程

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
    }
}

image

start() 解读

不能重复start()

public class CantStartTwice {
    public static void main(String[] args) {
        Thread thread = new Thread();
        thread.start();
        thread.start();
    }
}

image
源码解析:

  1. 启动新线程检查线程状态
    image

  2. 加入线程组

  3. 调用start0()

Run()解读

posted @ 2021-12-22 23:33  Zhbeii  阅读(31)  评论(0)    收藏  举报