3

I am trying to understand the behaviour of the executor service relative to shutdown. The documentation says that the application won't terminate unless there is a shutdown() call - but in this simple example. It exits after one minute precisely. Any idea?

 Runnable r = new Runnable() {
            @Override
            public void run() {
                Print.println("do nothing");
            }
        };
        ThreadFactory TF = (Runnable run) -> new Thread(run);
        ExecutorService exec = Executors.newCachedThreadPool(TF);
        exec.submit(r);

returns this: 11:34:00.421 : Thread-0: do nothing BUILD SUCCESSFUL (total time: 1 minute 0 seconds)

EKK
  • 171
  • 2
  • 12
  • Well, if you are running this in eclipse or some other IDE, you should see a red button at the right top corner of the console indicating that the process has not yet exited. – Chetan Kinger Mar 17 '15 at 11:41
  • running it from Netbeans - and i can confirm that the process has exited. So this is a netbeans feature? – EKK Mar 17 '15 at 11:44
  • Are you calling system.exit somewhere in your code explicitly? Btw how are you confirming that your process has exited? A build successfull message is no indication of this. – Chetan Kinger Mar 17 '15 at 11:45
  • Yes I confirm it has exited - there is no call to System.exit() and I also checked that running the jar of this small code directly from the command line (i.e. not from Netbeans) produced the same effect – EKK Mar 17 '15 at 11:47
  • Well, the only explanation there is left is that all the threads in your thread pool time out very quickly for you to have the need to call the shutdown method. Even then, calling shutdown is highly recommended. – Chetan Kinger Mar 17 '15 at 11:55

1 Answers1

3

You are using CachedThreadPool. It keeps the thread alive for 60 secs so that next subsequent tasks do not waste time in creating new thread resource. http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html

The internal code -

public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

You should call shutdown() once the job is done.

hemant1900
  • 1,226
  • 8
  • 9
  • I had an entire discussion yesterday on a related topic yesterday -http://stackoverflow.com/questions/29074604/threadpool-executor-on-both-runnable-and-callable-that-can-exits-automatical - The conclusion was that ExecutorService could not figure out when all threads were done - and that the ExecutorService could not shutdown automatically - but apparently it is possible - which is what CachedThreadPool is doing. – EKK Mar 17 '15 at 12:08
  • I understand that it is faster to call shutdown() and not wait for 60sec - but if time is not a concern - why should I call shutdown then? why not just set the timeout. Also the argument of yesterday was that an automatic shutdown could potentially miss threads on a timer - But it does not seem to be the case with the CachedThreadPool (I just ran a little script where a thread fire a task on a timer - and in this case the timeout does not apply). – EKK Mar 17 '15 at 12:13
  • 1
    For CachedThreadPool the core thread pool size is 0. That means as soon as the 60 secs timeout happens the pool has no thread running through it. Now this is similar to shutdown but not exactly shutdown because if you submit a task after 60 seconds the pool will still accept it and create a new thread to run that task. - But if you call shutdown() then pool will allow current tasks to finish and will no accept new tasks. – hemant1900 Mar 17 '15 at 12:23
  • 1
    So I think you are mixing two things here - pool can have no threads running in it because the core pool size is 0 which means JVM can shutdown as there are no active threads. But calling shutdown on thread pool executor means letting the executor know that no further tasks are coming so finish the current tasks and stop the threads even if core pool size is more that 0. – hemant1900 Mar 17 '15 at 12:27