标签:
这一章节我们来讨论一下守护线程。
1.特性
守护线程是需要等待其他用户线程结束后才结束的线程,俗称保姆线程
2.源码解析
/** * Marks this thread as either a {@linkplain #isDaemon daemon} thread * or a user thread. The Java Virtual Machine exits when the only * threads running are all daemon threads. * * <p> This method must be invoked before the thread is started. * * @param on * if {@code true}, marks this thread as a daemon thread * * @throws IllegalThreadStateException * if this thread is {@linkplain #isAlive alive} * * @throws SecurityException * if {@link #checkAccess} determines that the current * thread cannot modify this thread */ public final void setDaemon(boolean on) { checkAccess(); if (isAlive()) { throw new IllegalThreadStateException(); } daemon = on; }
我们从注释那里可以看到,jvm是在所有前台的用户线程完毕后才退出的,而这里面不管后台开了多少个守护线程
而且设置守护线程必须在start()之前
3.演示守护线程
package com.ray.deepintothread.ch01.topic_17; public class DaemonSample2 { public static void main(String[] args) throws InterruptedException { ThreadFour threadFour = new ThreadFour(); threadFour.start(); ThreadTwo threadTwo = new ThreadTwo(); threadTwo.setDaemon(true); threadTwo.start(); } } class ThreadFour extends Thread { @Override public void run() { while (true) { try { sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class ThreadTwo extends Thread { @Override public void run() { System.out.println(Thread.currentThread().getName() + " start"); } }
Thread-1 start
4.守护线程的坑
需要我们切记的一点是:不要把逻辑处理放到守护线程里面
一般守护线程都是放调度器一类的东西
代码清单:
package com.ray.deepintothread.ch01.topic_17; public class DaemonSample { public static void main(String[] args) throws InterruptedException { Thread.sleep(1000); ThreadOne threadOne = new ThreadOne(); threadOne.setDaemon(true); threadOne.start(); } } class ThreadOne extends Thread { @Override public void run() { for (int i = 0; i < 5; i++) { System.out.println(i); } } }输出:
(无)
为什么没有输出呢?
上面的执行是这样的:
(1)main线程起来,他的工作只是等待1秒
(2)在main线程起来的同时,另一个线程也起来,而且被设置为守护线程
(3)当main线程的等待时间结束,那么整个程序的用户就已经结束了,剩下的只是守护线程
(4)当main线程结束的同时,守护线程也结束,因此来不及打印,就已经把线程关掉了
因此,上面是打印不了东西的,同理,不要把逻辑处理放到守护线程里面
总结:这一章节介绍了一下守护线程以及他的坑。
我的github:https://github.com/raylee2015/DeepIntoThread
标签:
原文地址:http://blog.csdn.net/raylee2007/article/details/51204326