[concurrency-interest] Executors: Wrapper to cancel tasks in shutdownNow()?

Tim Peierls tim at peierls.net
Wed Apr 14 11:41:11 EDT 2010


It's not hard to create a forwarding wrapper with AbstractExecutorService
(although it would help to have a standard ForwardingExecutorService), but
why go to the trouble of burning the decision of whether to cancel the tasks
returned by shutdownNow into the executor service at construction, when you
can have a simple utility method that lets you decide at the moment of
shutdown?

    Util.shutdownNow(exec, cancelUnstartedTasks);

It's a decision that you'd want to make as late as possible, I would think.
And it hardly seems worth standardizing a simple for-loop. I could see
sticking it in as example code in ExecutorService.

--tim

On Wed, Apr 14, 2010 at 11:05 AM, Bryan Thompson <bryan at systap.com> wrote:

>  Yes it would.
>
> This sort of thing is not really called out in the javdoc for
> ExecutorService#shutdownNow().  I was thinking that it would be nice if
> people could use a standard wrapper ala the Executors delegation patterns
> for this.  That might help to reduce problems whose cause is a task which
> was never cancelled due to shutdownNow().
>
> Bryan
>
>  ------------------------------
> *From:* tpeierls at gmail.com [mailto:tpeierls at gmail.com] *On Behalf Of *Tim
> Peierls
> *Sent:* Wednesday, April 14, 2010 11:01 AM
>
> *To:* Bryan Thompson
> *Cc:* concurrency-interest at cs.oswego.edu
> *Subject:* Re: [concurrency-interest] Executors: Wrapper to cancel tasks
> in shutdownNow()?
>
> Right, sorry. Would something like this do the trick for you?
>
> public class Util {
>     private Util() { /* uninstantiable */ }
>
>     public static void shutdownNowCancelingUnstarted(ExecutorService exec)
> {
>         for (Runnable unstarted : exec.shutdownNow()) {
>             if (unstarted instanceof RunnableFuture) {
>                 RunnableFuture<?> task = (RunnableFuture<?>) unstarted;
>                 task.cancel(false);
>             }
>         }
>     }
> }
>
> --tim
>
> On Wed, Apr 14, 2010 at 10:42 AM, Bryan Thompson <bryan at systap.com> wrote:
>
>>  Yes, but threads can already be waiting on their Future's, right? Bryan
>>
>>  ------------------------------
>> *From:* tpeierls at gmail.com [mailto:tpeierls at gmail.com] *On Behalf Of *Tim
>> Peierls
>> *Sent:* Wednesday, April 14, 2010 10:42 AM
>> *To:* Bryan Thompson
>> *Cc:* concurrency-interest at cs.oswego.edu
>> *Subject:* Re: [concurrency-interest] Executors: Wrapper to cancel tasks
>> in shutdownNow()?
>>
>>   The tasks returned by shutdownNow haven't begun execution, so there's
>> no need to cancel them.
>>
>> --tim
>>
>> On Wed, Apr 14, 2010 at 9:53 AM, Bryan Thompson <bryan at systap.com> wrote:
>>
>>> Hello,
>>>
>>> Is there some handy delegation pattern available, such as found on
>>> Executors, to wrap an ExecutorService implementation such that
>>> ExecutorService#shutdownNow() cancels each task in the returned list?  While
>>> this might not always be want people want to do, it does seem to be a very
>>> common pattern.
>>>
>>> Thanks,
>>> Bryan
>>> _______________________________________________
>>> Concurrency-interest mailing list
>>> Concurrency-interest at cs.oswego.edu
>>> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>>>
>>
>>
>> _______________________________________________
>> Concurrency-interest mailing list
>> Concurrency-interest at cs.oswego.edu
>> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20100414/a76df23d/attachment-0001.html>


More information about the Concurrency-interest mailing list