[concurrency-interest] ScheduledThreadPoolExecutor thread timeout

Robert J. Saulnier robert.j.saulnier at gmail.com
Mon Feb 3 13:49:09 EST 2014


You don't need to wait for the Future to complete, you can shutdown the
executor right after scheduling the task(s) and these scheduled tasks will
still be executed.

So I believe my example meets the OP's requirements.


On Mon, Feb 3, 2014 at 2:34 PM, √iktor Ҡlang <viktor.klang at gmail.com> wrote:

>
>
>
> On Mon, Feb 3, 2014 at 7:25 PM, Robert J. Saulnier <
> robert.j.saulnier at gmail.com> wrote:
>
>> ScheduledExecutorService executor =
>> Executors.newSingleThreadScheduledExecutor();
>>
>> executor.schedule(() -> { System.out.println("hey"); }, 1L,
>> TimeUnit.SECONDS);
>>
>> executor.shutdown();
>>
>
> Sadly in JDK8 ScheduledFuture is not a CompletableFuture, otherwise you'd
> just attach the shutdown to be executed when the Future completes.
>
>
>>
>>
>>
>> On Mon, Feb 3, 2014 at 2:02 PM, Zhong Yu <zhong.j.yu at gmail.com> wrote:
>>
>>> Assume the task does not know it's the last one...
>>>
>>> On Mon, Feb 3, 2014 at 8:06 AM, Dr Heinz M. Kabutz
>>> <heinz at javaspecialists.eu> wrote:
>>> > How about doing this for a once-off scheduled task?
>>> >
>>> > import java.util.concurrent.*;
>>> >
>>> > public class ForeverYoung {
>>> >
>>> >  public static void main(String[] args) {
>>> >    ScheduledThreadPoolExecutor exec =
>>> >        new ScheduledThreadPoolExecutor(1);
>>> >    exec.schedule(() -> {
>>> >      System.out.println("done");
>>> >      exec.shutdown();
>>> >    },
>>> >        1, TimeUnit.SECONDS
>>> >    );
>>> >  }
>>> > }
>>> >
>>> > Regards
>>> >
>>> > Heinz
>>> > --
>>> > Dr Heinz M. Kabutz (PhD CompSci)
>>> > Author of "The Java(tm) Specialists' Newsletter"
>>> > Oracle Java Champion 2005-2013
>>> > JavaOne Rock Star Speaker 2012
>>> > http://www.javaspecialists.eu
>>> > Tel: +30 69 75 595 262
>>> > Skype: kabutz
>>> >
>>> >
>>> >
>>> > Zhong Yu wrote:
>>> >>
>>> >> In this simple example, the JVM never exits:
>>> >>
>>> >>     public static void main(String[] args)
>>> >>     {
>>> >>         ScheduledThreadPoolExecutor exec =
>>> >>             new ScheduledThreadPoolExecutor(1);
>>> >>         exec.schedule( ()-> System.out.println("done"),
>>> >>             1, TimeUnit.SECONDS );
>>> >>     }
>>> >>
>>> >> because the core thread cannot timeout.
>>> >>
>>> >> This creates a problem in a hot-reload environment, where each new app
>>> >> instance creates a new scheduler thread that never terminates. Is
>>> >> there a workaround to the effect that the thread can timeout? Thanks.
>>> >>
>>> >> Zhong Yu
>>> >> _______________________________________________
>>> >> 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
>>>
>>
>>
>> _______________________________________________
>> Concurrency-interest mailing list
>> Concurrency-interest at cs.oswego.edu
>> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>>
>>
>
>
> --
> Cheers,
>>
> * ——————— **Viktor Klang*
> *Chief Architect - **Typesafe <http://www.typesafe.com/>*
>
>  Twitter: @viktorklang
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20140203/9ee117ac/attachment.html>


More information about the Concurrency-interest mailing list