守護進程,顧名思義就是默默在後台運行的進程,當JVM上沒有其他進程運行時運行的進程,最典型的一個列子就是JVM自帶的垃圾清理機,他就是在其他程序不運行的時候占用CPU來進行清理內存垃圾,因此他具有很強的不確定性,因為你不知道啥時候他就會運行,也不知道啥時候他不會運行,因此適合做一個不太重要的清理工作或者服務器上的監聽工作。
Java多線程從簡單到復雜 http://www.linuxidc.com/Linux/2014-07/104435.htm
Java多線程經典案例 http://www.linuxidc.com/Linux/2014-06/103458.htm
Java多線程:ReentrantReadWriteLock讀寫鎖的使用 http://www.linuxidc.com/Linux/2014-06/103457.htm
Java內存映射文件實現多線程下載 http://www.linuxidc.com/Linux/2014-05/102201.htm
Java多線程:一道阿裡面試題的分析與應對 http://www.linuxidc.com/Linux/2014-03/98715.htm
Java中兩種實現多線程方式的對比分析 http://www.linuxidc.com/Linux/2013-12/93690.htm
下面我們使用JVM的daemon來寫一個小例子,實現的效果如下
In this recipe, we will learn how to create a daemon thread developing an example with two threads; one user thread that writes events on a queue and a daemon one that cleans that queue, removing the events which were generated more than 10 seconds ago。
這裡的隊列是一個雙向列表。代碼如下
package com.bird.concursey;
import java.util.Date;
import java.util.Deque;
import java.util.concurrent.TimeUnit;
public class WriterTask implements Runnable {
public WriterTask(Deque<Event> deque) {
this.deque = deque;
}
// 這個為雙向隊列
private Deque<Event> deque;
public Deque<Event> getDeque() {
return deque;
}
public void setDeque(Deque<Event> deque) {
this.deque = deque;
}
@Override
public void run() {
for(int i = 0; i < 100; i++) {
Event event = new Event();
event.setDate(new Date());
event.setEvent("The Thread " + Thread.currentThread().getId() + " has generated a event");
deque.addFirst(event);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package com.bird.concursey;
import java.util.Date;
import java.util.Deque;
public class CleanerTask extends Thread {
private Deque<Event> deque;
public CleanerTask(Deque<Event> deque) {
this.deque = deque;
//設置為守護進程
setDaemon(true);
}
@Override
public void run() {
while(true) {
Date date = new Date();
clean(date);
}
}
private void clean(Date date) {
long difference = 0;
boolean delete = false;
if(deque.size() == 0) {
return;
}
do {
Event e = deque.getLast();
difference = date.getTime() - e.getDate().getTime();
if(difference > 10000) {
System.out.println("cleaner " + e.getEvent());
deque.removeLast();
delete = true;
}
}while(difference > 10000);
if(delete) {
System.out.println("cleaner : the size of the deque " + deque.size());
}
}
public Deque<Event> getDeque() {
return deque;
}
public void setDeque(Deque<Event> deque) {
this.deque = deque;
}
}
package com.bird.concursey;
import java.util.ArrayDeque;
import java.util.Date;
import java.util.Deque;
public class Event {
private Date date;
private String event;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getEvent() {
return event;
}
public void setEvent(String event) {
this.event = event;
}
public static void main(String[] args) {
Deque<Event> deque = new ArrayDeque<Event>();
WriterTask writer = new WriterTask(deque);
for(int i = 0; i < 3; i++) {
Thread thread = new Thread(writer);
thread.start();
}
CleanerTask cleaner = new CleanerTask(deque);
cleaner.start();
}
}
運行結果就是
cleaner The Thread 12 has generated a event
cleaner The Thread 11 has generated a event
cleaner : the size of the deque 27
cleaner The Thread 13 has generated a event
cleaner : the size of the deque 26
cleaner The Thread 13 has generated a event
cleaner The Thread 12 has generated a event
cleaner The Thread 11 has generated a event
cleaner : the size of the deque 26
cleaner The Thread 12 has generated a e
這個隊列總是維護在25-30左右,因為上來三個線程創建了30個事件,然後等創建事件的線程休息的時候,daemon線程就會占用CPU time 來運行清理建立好的事件,這樣就會讓隊列一直維護在25-30左右,最關鍵的一句//設置為守護進程 setDaemon(true);必須在start之前設置,因為線程一旦運行起來了就不能更改他的狀態,這樣就開始了監控的狀態,守護進程就OK了.