Best way to remove a scheduled task with ScheduledThreadPoolExecutor

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Best way to remove a scheduled task with ScheduledThreadPoolExecutor

syg6
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
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Best way to remove a scheduled task with ScheduledThreadPoolExecutor

Holger Hoffstätte-2
Robert Bowen wrote:
> 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?

You cannot - that method does not work as expected and contains an
internal bug that got fixed in Mustang b71. Check the mailing list
archives from around last December/January, there was a fairly long
discussion about this. Summary: just use cancel() on your returned Futures
- this will let the tasks drop out of the queue eventually.

Holger
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Best way to remove a scheduled task with ScheduledThreadPoolExecutor

Joe Bowbeer
After cancel() you *can* call STPE.purge() to remove it ASAP.  But
this is probably not necessary unless you're wasting lots and lots of
tasks.

On 10/10/06, Holger Hoffstätte <[hidden email]> wrote:

> Robert Bowen wrote:
> > 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?
>
> You cannot - that method does not work as expected and contains an
> internal bug that got fixed in Mustang b71. Check the mailing list
> archives from around last December/January, there was a fairly long
> discussion about this. Summary: just use cancel() on your returned Futures
> - this will let the tasks drop out of the queue eventually.
>
> Holger
>

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Best way to remove a scheduled task with ScheduledThreadPoolExecutor

syg6
In reply to this post by syg6
Hmmm ... I'm at home now and don't have my code in front of me but ... if I am not mistaken, when you call ScheduledThreadPoolExecutor.scheduleAtFixedRate(Runnable) what you're left with is an Executor with Future Tasks, each one an instance of your Runnable. So my question remains, how do I get access to each individual Runnable / Future Task, so I can call cancel() on it?

Maybe I'll see it more clearly when I have the code in front of me ...

Thanks for your help!
Bob

----- Original Message ----
From: Joe Bowbeer <[hidden email]>
To: [hidden email]
Sent: Tuesday, October 10, 2006 6:08:39 PM
Subject: Re: [concurrency-interest] Best way to remove a scheduled task with ScheduledThreadPoolExecutor

After cancel() you *can* call STPE.purge() to remove it ASAP.  But
this is probably not necessary unless you're wasting lots and lots of
tasks.

On 10/10/06, Holger Hoffstätte <[hidden email]> wrote:

> Robert Bowen wrote:
> > 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?
>
> You cannot - that method does not work as expected and contains an
> internal bug that got fixed in Mustang b71. Check the mailing list
> archives from around last December/January, there was a fairly long
> discussion about this. Summary: just use cancel() on your returned Futures
> - this will let the tasks drop out of the queue eventually.
>
> Holger
>

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest



_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Best way to remove a scheduled task with ScheduledThreadPoolExecutor

Joe Bowbeer
scheduleAtFixedRate returns a (Scheduled)Future instance associated
with your Runnable.  This Future is what you cancel.

If you want to cancel each and every one, you can shutdownNow the
executorService.

On 10/10/06, Robert Bowen <[hidden email]> wrote:

>
> Hmmm ... I'm at home now and don't have my code in front of me but ... if I
> am not mistaken, when you call
> ScheduledThreadPoolExecutor.scheduleAtFixedRate(Runnable)
> what you're left with is an Executor with Future Tasks, each one an instance
> of your Runnable. So my question remains, how do I get access to each
> individual Runnable / Future Task, so I can call cancel() on it?
>
> Maybe I'll see it more clearly when I have the code in front of me ...
>
> Thanks for your help!
> Bob
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Best way to remove a scheduled task with ScheduledThreadPoolExecutor

syg6
In reply to this post by syg6
Right you are! Problem solved! They seemed to have thought of everything when developing this API!

Thanks for all your help, it saved me a lot of time!

Bob

----- Original Message ----
From: Joe Bowbeer <[hidden email]>
To: [hidden email]
Sent: Tuesday, October 10, 2006 9:25:49 PM
Subject: Re: [concurrency-interest] Best way to remove a scheduled task with ScheduledThreadPoolExecutor

scheduleAtFixedRate returns a (Scheduled)Future instance associated
with your Runnable.  This Future is what you cancel.

If you want to cancel each and every one, you can shutdownNow the
executorService.

On 10/10/06, Robert Bowen <[hidden email]> wrote:

>
> Hmmm ... I'm at home now and don't have my code in front of me but ... if I
> am not mistaken, when you call
> ScheduledThreadPoolExecutor.scheduleAtFixedRate(Runnable)
> what you're left with is an Executor with Future Tasks, each one an instance
> of your Runnable. So my question remains, how do I get access to each
> individual Runnable / Future Task, so I can call cancel() on it?
>
> Maybe I'll see it more clearly when I have the code in front of me ...
>
> Thanks for your help!
> Bob
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest



_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Best way to remove a scheduled task withScheduledThreadPoolExecutor

David Holmes-3
Note however, that in Java 5, there is an issue with cancel() decreasing the number of worker threads in the pool. This is fixed in Java 6.
 
David Holmes
-----Original Message-----
From: [hidden email] [mailto:[hidden email]]On Behalf Of Robert Bowen
Sent: Wednesday, 11 October 2006 6:59 PM
To: Joe Bowbeer; [hidden email]
Subject: Re: [concurrency-interest] Best way to remove a scheduled task withScheduledThreadPoolExecutor

Right you are! Problem solved! They seemed to have thought of everything when developing this API!

Thanks for all your help, it saved me a lot of time!

Bob

----- Original Message ----
From: Joe Bowbeer <[hidden email]>
To: [hidden email]
Sent: Tuesday, October 10, 2006 9:25:49 PM
Subject: Re: [concurrency-interest] Best way to remove a scheduled task with ScheduledThreadPoolExecutor

scheduleAtFixedRate returns a (Scheduled)Future instance associated
with your Runnable.  This Future is what you cancel.

If you want to cancel each and every one, you can shutdownNow the
executorService.

On 10/10/06, Robert Bowen <[hidden email]> wrote:

>
> Hmmm ... I'm at home now and don't have my code in front of me but ... if I
> am not mistaken, when you call
> ScheduledThreadPoolExecutor.scheduleAtFixedRate(Runnable)
> what you're left with is an Executor with Future Tasks, each one an instance
> of your Runnable. So my question remains, how do I get access to each
> individual Runnable / Future Task, so I can call cancel() on it?
>
> Maybe I'll see it more clearly when I have the code in front of me ...
>
> Thanks for your help!
> Bob
>
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest



_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest