Thread-守护线程

private static void doemon(){

    // 守护线程,当程序中只剩下守护线程时程序终止。
    Thread t = new Thread(() -> {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("i=" + i);
        }
    });
    //t.setDaemon(true);//this is set t thread as a daemon thread.
    t.start();
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("main thread exit.");
}

猜你喜欢

转载自blog.csdn.net/qq_37751454/article/details/80019460