logo       

Best way to remove a scheduled task with ScheduledThreadPoolExecutor: msg#00008

java.jsr.166-concurrency

Subject: Best way to remove a scheduled task with ScheduledThreadPoolExecutor

Hello again all,

I am using a ScheduledThreadPoolExecutor to schedule Runnable tasks. I would like to be able to remove tasks as well. I see in the API that ScheduledThreadPoolExecutor has a remove() method which takes a Runnable as a parameter. Fantastic! How do I tell it which Runnable I want to remove?

I've tried getting the ScheduledThreadPoolExecutor's BlockingQueue and iterating over it:

BlockingQueue bq = stpe.getQueue();
while (it.hasNext())
{
  MyRunnable mr = (MyRunnable)it.next();
}

but as soon as I call it.next() I get a ConcurrentModificationException. Yikes.

How else can I do this? Removing all of the elements (using drainTo()?) off the BlockingQueue and re-scheduling all of the remaining ScheduledThreadPoolExecutor tasks? This doesn't sound like a good idea ...

Many thanks!
Bob

_______________________________________________
Concurrency-interest mailing list
Concurrency-interest@xxxxxxxxxxxxxxxxxxxx
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise