I am implementing a thread pooling mechanism in which I'd like to execute tasks of varying priorities. I'd like to have a nice mechanism whereby I can submit a high priority task to the service and have it be scheduled before other tasks. The priority of the task is an intrinsic property of the task itself (whether I express that task as a Callable
or a Runnable
is not important to me).
Now, superficially it looks like I could use a PriorityBlockingQueue
as the task queue in my ThreadPoolExecutor
, but that queue contains Runnable
objects, which may or may not be the Runnable
tasks I've submitted to it. Moreover, if I've submitted Callable
tasks, it's not clear how this would ever map.
Is there a way to do this? I'd really rather not roll my own for this, since I'm far more likely to get it wrong that way.
(An aside; yes, I'm aware of the possibility of starvation for lower-priority jobs in something like this. Extra points (?!) for solutions that have a reasonable guarantee of fairness)