黑马程序员技术交流社区

标题: 线程 [打印本页]

作者: 杨海鹏    时间: 2016-3-27 13:26
标题: 线程
package com.bingfeng.huo;

public class TestSleep {

        public static void main(String[] args) {
                Thread t1 = new Thread() {

                        @Override
                        public void run() {
                                this.setName("守护线程。");
                                for (int i = 0; i < 2; i++) {
                                        try {
                                                Thread.sleep(10);
                                        } catch (InterruptedException e) {
                                                e.printStackTrace();
                                        }
                                        System.out.println(getName() + "..." + i);
                                }
                        }

                };
                Thread t2 = new Thread(new Runnable() {

                        @Override
                        public void run() {
                                for (int i = 0; i < 50; i++) {
                                        System.out.println(Thread.currentThread().getName() + "...." + i);
                                        try {
                                                Thread.sleep(10);
                                        } catch (InterruptedException e) {
                                                e.printStackTrace();
                                        }
                                }

                        }

                });
                t1.setDaemon(true);
                t1.start();
                t2.start();
               
       
        }

}
这个程序为什么守护线程已经结束了,另一个线程还在跑?




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2