守护线程(Daemon)<wbr style="line-height:25px"><br style="line-height:25px"><wbr style="line-height:25px">Java有两种Thread:“<span style="color:#99cc00; line-height:25px">守护线程Daemon</span>”与“<span style="color:#808000; line-height:25px">用户线程User</span>”。<wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#003366; line-height:25px">任何线程都可以是“</span><span style="color:#99cc00; line-height:25px">守护线程Daemon</span><span style="color:#003366; line-height:25px">”或“</span><span style="color:#808000; line-height:25px">用户线程User</span><span style="color:#003366; line-height:25px">”。他们在几乎每个方面都是相同的,</span><br style="line-height:25px"> 唯一的区别是用来判断虚拟机何时离开:<br style="line-height:25px"><span style="line-height:25px">用户线程</span>:<span style="color:#000080; line-height:25px">Java虚拟机在它所有非守护线程已经离开后自动离开。</span><br style="line-height:25px"><span style="line-height:25px">守护线程</span><span style="color:#003366; line-height:25px">:</span><span style="color:#000080; line-height:25px">守护线程则是用来服务用户线程的,如果没有其他用户线程在运行,那么就没有可服务对象,也就没有理由继续下去。</span><br style="line-height:25px"> 上面是网上的说法。我觉得它的阐述有些地方不妥。我觉得应该是这样的:<br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">唯一的区别是JVM虚拟机根据是否有用户线程来判断的是否该进程该结束</span><br style="line-height:25px"><span style="line-height:25px">用户线程:</span><span style="color:#000080; line-height:25px">只要有任何用户线程在运行,该进程就不该结束,否则就应该结束。</span><br style="line-height:25px"><span style="line-height:25px">守护线程:</span><span style="color:#000080; line-height:25px">守护线程则是用来服务用户线程的。如果程序所在的进程没有其他用户线程在运行,那么就没有可服务对象,也就没有理由继续下去,该线程也就会结束,同时程序所在进程也结束。</span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#ff6600; line-height:25px">setDaemon(booleanon)</span>方法可以方便的设置线程的<span style="color:#ff9900; line-height:25px">Daemon</span>模式,<span style="color:#0000ff; line-height:25px">true</span>为<span style="color:#99cc00; line-height:25px">Daemon模式</span>,<span style="color:#0000ff; line-height:25px">false</span>为<span style="color:#808000; line-height:25px">User模式</span>。<br style="line-height:25px"> setDaemon(booleanon)方法必须在线程启动之前调用,当线程正在运行时调用会产生异常。<br style="line-height:25px"> isDaemon方法将测试该线程是否为守护线程。值得一提的是,<span style="color:#000080; line-height:25px">当你在一个守护线程中产生了其他线程,<br style="line-height:25px"> 那么这些新产生的线程不用设置Daemon属性,<wbr style="line-height:25px">默认是守护线程<wbr style="line-height:25px">,当然也可以setDaemon设置为用户线程。</wbr></wbr></span><br style="line-height:25px"> 当在用户线程中创建线程时,<span style="color:#000080; line-height:25px">它默认是用户线程,也可以setDaemon设置为守护线程</span><br style="line-height:25px"> 例:我们所熟悉的Java垃圾回收线程就是一个典型的守护线程,当我们的程序中不再有任何运行中的Thread,<br style="line-height:25px"> 程序就不会再产生垃圾,垃圾回收器也就无事可做,所以当垃圾回收线程是Java虚拟机上仅剩的线程时,<br style="line-height:25px"> Java虚拟机会自动离开。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">示例1</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.io.IOException;</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px"> *守护线程在没有用户线程可服务时自动离开<br style="line-height:25px"> */</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#ff6600; line-height:25px">TestMain4</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">Thread{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicTestMain4(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px"> *线程的run方法,它将和其他线程同时运行<br style="line-height:25px"> */</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">run</span><span style="color:#3366ff; line-height:25px">(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">for</span><span style="color:#3366ff; line-height:25px">(inti=1;i<=100;i++){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Thread.sleep(100);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">catch</span><span style="color:#3366ff; line-height:25px">(InterruptedExceptionex){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ex.printStackTrace();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println(i);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicstaticvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">main</span><span style="color:#3366ff; line-height:25px">(String[]args){//这里是个主线程</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">TestMain4test=newTestMain4();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">test.setDaemon(true);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">test.start();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("isDaemon="+test.isDaemon());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">System.in.read()</span><span style="color:#808080; line-height:25px">;//接受输入,使程序在此停顿,一旦接收到用户输入,main线程结束,守护线程自动结束</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}catch(IOExceptionex){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ex.printStackTrace();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="line-height:25px">注意</span>:<span style="color:#ff6600; line-height:25px">main(String[]args)</span><span style="color:#000080; line-height:25px">这里是个主线程调用的,它是个用户线程。</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">实例2</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">Test</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px"> *@paramargs<br style="line-height:25px"> */</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicstaticvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">main</span><span style="color:#3366ff; line-height:25px">(String[]args){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Threadt=newThread(newMyThread());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">t.setDaemon(true);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">t.start();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Threadt2=newThread(newMyThread());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">t2.start();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px">MyThread</span><span style="color:#ff6600; line-height:25px">implements</span><span style="color:#3366ff; line-height:25px">Runnable</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">longid=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">staticlongcount=0;</span><br style="line-height:25px"><span style="color:#ff6600; line-height:25px">MyThread</span><span style="color:#3366ff; line-height:25px">()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">id=count;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">count++;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">run()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">while</span><span style="color:#3366ff; line-height:25px">(true)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("run"+id);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Thread.sleep(100);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">catch</span><span style="color:#3366ff; line-height:25px">(InterruptedExceptione)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">e.printStackTrace();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px">:如果没有t2用户线程进程,t线程在main线程退出后,就没线程可服务了,t线程会结束,<br style="line-height:25px"> 程序所在进程也同时结束了。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
守护线程
最新推荐文章于 2025-06-20 10:13:14 发布