我有一个包含任务的BlockingQueue
,并且只有一个守护进程线程执行它们:
public class TaskManager {
private final BlockingQueue<Task> taskQueue;
public TaskManager() {
this.taskQueue = new ArrayBlockingQueue<>(5); //Max 5 tasks waiting
Thread taskRunner = new Thread(this::runTask);
taskRunner.setDaemon(true); //Infinite loop inside shouldn't prevent JVM shutdown
taskRunner.start();
}
//Called async
public void enqueueTask(Task task) {
boolean added = false;
try {
added = taskQueue.offer(task, 5, TimeUnit.SECONDS);
} catch (InterruptedException e) {/*no-op*/}
if (!added) {
publishEvent(new Fail(task)); //Async notify the client their task won't run
}
}
//Also called async
public void dequeueTask(Task task) {
this.taskQueue.remove(task);
}
void runTask() {
while (true) {
try {
Task task = taskQueue.take(); //Blocks indefinitely
doStuff(task);
} catch (InterruptedException e) {
//Can only be a spurious interruption. Ignore.
}
}
}
}
看起来没什么特别的.但.感觉就像我通过自己管理队列和守护进程来重新发明轮子.我本以为Executor(Service)
分会很容易做到这一点.我可以有一个有1个线程的ThreadPoolExecutor
,给它一个有界队列.它为我提供了一个将任务出列的remove
方法.但是,如果排队花费的时间太长(在我的示例中,taskQueue.offer()
上的5s超时),似乎没有办法超时.
我做的事是不是已经合法了只是我想多了?如果没有,我可以用Executor
来代替它吗?