[concurrency-interest] ScheduledThreadPoolExecutor threadtimeout

David Holmes davidcholmes at aapt.net.au
Sat Feb 1 02:34:14 EST 2014


Zhong Yu writes:
> On Sat, Feb 1, 2014 at 1:09 AM, David Holmes
> <davidcholmes at aapt.net.au> wrote:
> > I wrote:
> >> Zhong Yu writes:
> >> >
> >> > 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.
> >>
> >> There's no way to have the thread timeout and still have a
> >> functioning executor when there is a task waiting to be
> >> scheduled. If you don't care about it functioning correctly then
> >> call allowCoreThreadTimeOut(true) and set your desired timeout.
> >
> > Correction the idle timeout won't kick-in if there is a task
> waiting to be
> > scheduled. Once there are no tasks then the core thread can timeout.
>
> But the javadoc explicitly warns that
>
> > it is almost never a good idea to set corePoolSize to zero or
> use allowCoreThreadTimeOut because this may leave the pool
> without threads to handle tasks once they become eligible to run.
>
> is the current implementation more robust than that?

I think it is in the sense that if there is a task waiting to become
eligible to run then there is a worker thread that is waiting for that time
to arrive. But the details are complex and there may be circumstances where
tasks are not executed as expected.

David

> Zhong Yu
>
>
>
> >
> > David
> >
> >> David
> >>
> >> > 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



More information about the Concurrency-interest mailing list