Add threadPriority argument to createQueue.

This stops ScheduledThreadPoolExecutor API usage from leaking beyond WorkQueue.

Change-Id: I9c7b141272a6d547c4f2c39c28a5ea60ada944d7
This commit is contained in:
Han-Wen Nienhuys
2017-06-13 17:48:36 +02:00
parent 008eb7c482
commit 4daf68c8b8
3 changed files with 23 additions and 39 deletions

View File

@@ -108,11 +108,25 @@ public class WorkQueue {
/** Create a new executor queue. */
public ScheduledThreadPoolExecutor createQueue(int poolsize, String prefix) {
final Executor r = new Executor(poolsize, prefix);
r.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
r.setExecuteExistingDelayedTasksAfterShutdownPolicy(true);
queues.add(r);
return r;
return createQueue(poolsize, prefix, Thread.NORM_PRIORITY);
}
public ScheduledThreadPoolExecutor createQueue(int poolsize, String prefix, int threadPriority) {
Executor executor = new Executor(poolsize, prefix);
executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(true);
queues.add(executor);
if (threadPriority != Thread.NORM_PRIORITY) {
ThreadFactory parent = executor.getThreadFactory();
executor.setThreadFactory(
task -> {
Thread t = parent.newThread(task);
t.setPriority(threadPriority);
return t;
});
}
return executor;
}
/** Get all of the tasks currently scheduled in any work queue. */