package com.jackson.deng.concurrent.chapter1.six;
import java.util.ArrayDeque;
import java.util.Date;
import java.util.Deque;
import java.util.concurrent.TimeUnit;
/**
* title : 守护线程的创建和运行
* desc : 模拟一个写任务进程和清除任务进程(守护线程,也是后台线程)
* process desc:
* 3个写任务进程负责到每隔1秒向双向队列deque的添加数据最前面
* 清理进程从开始运行的当前时间-写进程写到deque的进程时间>10秒,就把deque从后面进行移除
* @author jackson
*
*/
public class DaemonCreateAndRunnable {
/**
* 写任务进程
*/
public class WriterTask implements Runnable {
//双向队列
private Deque<Event> deque;
public WriterTask(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(System.currentTimeMillis()));
event.setEvent("The thread " + Thread.currentThread().getId() + "generator a event;");
deque.addFirst(event);
try {
TimeUnit.SECONDS.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public class CleanerTask extends Thread {
private Deque<Event> deque;
public CleanerTask(Deque<Event> deque) {
this.deque = deque;
setDaemon(true);//设置CleanerTask为守护线程
}
@Override
public void run() {
while (true) {
Date date = new Date(System.currentTimeMillis());
clean(date);
}
}
/**
* 守护线程cleaner运行后的10秒移除新增加到deque中的event
*/
private void clean(Date date) {
if (deque.size() <= 0) {
return;
}
long difference = 0l;
boolean delete = false;
while (true) {
Event e = deque.getLast();
difference = date.getTime() - e.getDate().getTime();
if (difference > 10000) {
System.out.println("Cleaner : " + e.getEvent());
deque.removeLast();
delete = true;
} else {
break;
}
}
if (delete) {
System.out.println("Deque : size of the thread is " + deque.size());
}
}
}
public static void main(String[] args) {
DaemonCreateAndRunnable dcr = new DaemonCreateAndRunnable();
Deque<Event> deque = new ArrayDeque<Event>();
for (int i = 0; i < 3; i++) {
Thread t = new Thread(dcr.new WriterTask(deque));
t.start();
}
Thread clean = dcr.new CleanerTask(deque);
clean.start();
}
}
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;
}
}
运行结果:
Cleaner : The thread 9generator a event;
Cleaner : The thread 10generator a event;
Deque : size of the thread is 27
Cleaner : The thread 8generator a event;
Cleaner : The thread 10generator a event;
Cleaner : The thread 9generator a event;
Deque : size of the thread is 27
Cleaner : The thread 10generator a event;
Cleaner : The thread 9generator a event;
Cleaner : The thread 8generator a event;
Deque : size of the thread is 27
Cleaner : The thread 8generator a event;
Cleaner : The thread 10generator a event;
Cleaner : The thread 9generator a event;
Deque : size of the thread is 27
Cleaner : The thread 9generator a event;
Cleaner : The thread 10generator a event;
Cleaner : The thread 8generator a event;
Deque : size of the thread is 27
Cleaner : The thread 9generator a event;
Cleaner : The thread 10generator a event;
Cleaner : The thread 8generator a event;
Deque : size of the thread is 27
Cleaner : The thread 9generator a event;
Deque : size of the thread is 29
Cleaner : The thread 10generator a event;
Cleaner : The thread 8generator a event;
Deque : size of the thread is 27
Cleaner : The thread 8generator a event;
Cleaner : The thread 10generator a event;
Cleaner : The thread 9generator a event;
Deque : size of the thread is 27
Cleaner : The thread 8generator a event;
Deque : size of the thread is 29
Cleaner : The thread 9generator a event;
Deque : size of the thread is 30
Cleaner : The thread 10generator a event;
Deque : size of the thread is 29
另外:http://blog.csdn.net/vernonzheng/article/details/8267541可以对Deque双向队列有个清楚点的认识