Java 大量延时执行任务新思DelayQueue
记得我初次发现这个东西的时候,那心情简直就是喜出望外,这个功能我曾冥思苦想大半年而没有结果,无意间发现JDK早已经想到并做非常好的实现了
以前开发中经常有这样一个需求,就是有很多事物是需要延时一定时间后执行的,这样的需求如果不多的话新开一个线程sleep 就行了,但当这样的事物比较多时新开线程就显得太奢侈了,并可能因为线程过多导致服务器崩溃的情况,DelayQueue 就这个家伙,只需要一个线程调度,就可以轻松搞定成千上万的这样的任务,哈哈,多的不说,先上demo
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static java.util.concurrent.TimeUnit.*;
class DelayedTask implements Runnable, Delayed {
private static int counter = 0;
protected static List<DelayedTask> sequence = new ArrayList<>();
private final int id = counter++;
private final int delayTime;
private final long triggerTime;
public DelayedTask(int delayInMillis) {
delayTime = delayInMillis;
triggerTime = System.nanoTime() + NANOSECONDS.convert(delayTime, MILLISECONDS);
sequence.add(this);
}
@Override
public int compareTo(Delayed o) {
DelayedTask that = (DelayedTask)o;
return triggerTime < that.triggerTime ? -1 : 1;
}
/**
* 剩余的延迟时间
*/
@Override
public long getDelay(TimeUnit unit) {
return unit.convert(triggerTime - System.nanoTime(), NANOSECONDS);
}
@Override
public void run() {
System.out.println(this + " ");
}
@Override
public String toString() {
return String.format("[%1$-4d]", delayTime) + " Task " + id;
}
public static class EndSentinel extends DelayedTask {
private ExecutorService exec;
public EndSentinel(int delay, ExecutorService exec) {
super(delay);
this.exec = exec;
}
@Override
public void run() {
System.out.println(this + " calling shutDownNow()");
exec.shutdownNow();
}
}
}
class DelayedTaskConsumer implements Runnable {
private DelayQueue<DelayedTask> tasks;
public DelayedTaskConsumer(DelayQueue<DelayedTask> tasks) {
this.tasks = tasks;
}
@Override
public void run() {
try {
while(!Thread.interrupted()) {
tasks.take().run();//run tasks with current thread.
}
} catch (InterruptedException e) {
// TODO: handle exception
}
System.out.println("Finished DelaytedTaskConsumer.");
}
}
public class DelayQueueDemo {
public static void main(String[] args) {
int maxDelayTime = 5000;//milliseconds
Random random = new Random(47);
ExecutorService exec = Executors.newCachedThreadPool();
DelayQueue<DelayedTask> queue = new DelayQueue<>();
//填充10个休眠时间随机的任务
for (int i = 0; i < 10; i++) {
queue.put(new DelayedTask(random.nextInt(maxDelayTime)));
}
//设置结束的时候。
queue.add(new DelayedTask.EndSentinel(maxDelayTime, exec));
exec.execute(new DelayedTaskConsumer(queue));
}
}