守护线程讲解
線程分用戶線程和守護線程
虛擬機必須確保用戶線程執行完畢
虛擬機不用等待守護線程執行完畢
后臺記錄操作日志,監控內存,垃圾回收
package com.wuming.state; //測試守護線程 //上帝守護你 public class TestDaemon {public static void main(String[] args) {God god = new God();You you = new You();Thread thread = new Thread(god);thread.setDaemon(true);//默認是false表示用戶線程,正常的線程都是用戶線程。。。thread.start();//上帝守護線程啟動new Thread(you).start();//你,用戶線程啟動} } //上帝 class God implements Runnable{/*** When an object implementing interface <code>Runnable</code> is used* to create a thread, starting the thread causes the object's* <code>run</code> method to be called in that separately executing* thread.* <p>* The general contract of the method <code>run</code> is that it may* take any action whatsoever.** @see Thread#run()*/@Overridepublic void run() {while(true){System.out.println("上帝保佑著你");}} } //你 class You implements Runnable{/*** When an object implementing interface <code>Runnable</code> is used* to create a thread, starting the thread causes the object's* <code>run</code> method to be called in that separately executing* thread.* <p>* The general contract of the method <code>run</code> is that it may* take any action whatsoever.** @see Thread#run()*/@Overridepublic void run() {for (int i = 0; i < 36500; i++) {System.out.println("你一生都開心的活著");}System.out.println("goodbye====world!!");} }總結
- 上一篇: C语言 函数不定长参数 ##__VA_A
- 下一篇: C语言 NULL 和 0 区别 - C语