守护线程setDaemon(true)


    public static void main(String[] args) {
        Thread p1 = new Thread(){
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println("go go go !!! ");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("111111");
            };
        };
        p1.start();

        // 守护线程
        Thread p2 = new Thread(){
            @Override
            public void run() {
                while(true){
                    System.out.println("to to to !!! ");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        p2.setDaemon(true);
        p2.start();
    }

你可能感兴趣的:(时间,守护线程,线程获取cpu时间)