[concurrency-interest] Removing jobs from ThreadPoolExecutor

Brian Goetz brian at quiotix.com
Fri Sep 2 12:46:32 EDT 2005

Let me see if I understand what you're doing:

- You have one executor
- It shares both high and low priority tasks
- You want to reap low-priority tasks if the queue hits some bound so 
that you can make room for more high priority tasks

Are you using a PriorityBlockingQueue for the tasks?  Is it bounded?

You might be able to do this with a custom BlockingQueue that derives 
most of its functionality from PriorityBlockingQueue, but whose offer() 
method kicks out old elements when the queue seems "too full".  (To do 
this, you'd have to iterate the queue; the PriorityBlockingDeque class 
from JSR166x would make this easier, because you could just delete the 
last element(s) to make some room.)  Then you wouldn't need a separate 
thread, because you'd intercept the offer() and only shed tasks when a 
new high priority task shows up and there are already too many tasks on 
the queue.

Norman Elton wrote:
> I'm using a ThreadPoolExecutor to handle incoming jobs. Jobs are  either 
> "low priority" or "high priority". I have a separate thread  that 
> monitors the queue to ensure that it does not get out of control.
> I'd like for the monitoring thread to remove all low priority jobs if  
> the queue gets above a certain size. Currently, I plan to loop  through 
> the queue, retrieved from ThreadPoolExecutor.getQueue(), and  call 
> ThreadPoolExecutor.remove() on all low priority jobs.
> Is this the best way to accomplish this? Will there be any  concurrency 
> issues here? It appears that if I remove a job that has  already been 
> executed, the method will simply return false.
> Thanks for any ideas,
> Norman
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

More information about the Concurrency-interest mailing list