Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop a ScheduledExecutorService?

The program finishes after nine prints:

class BeeperControl {      private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);      public void beep() {         final Runnable beeper = new Runnable() {             public void run() {                 System.out.println("beep");             }         };         final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(                 beeper, 1, 1, SECONDS);         scheduler.schedule(new Runnable() {             public void run() {                 beeperHandle.cancel(true);             }         }, 1 * 9, SECONDS);     }      public static void main(String[] args) {         BeeperControl bc = new BeeperControl();         bc.beep();     } } 

How to stop a process (i.e. java process in eclipse for example) because it does not stop after a time limit in 9 seconds?

like image 851
ferro Avatar asked Dec 10 '15 13:12

ferro


People also ask

How do I exit ScheduledExecutorService?

If - instead of cancelling the beeper - you simply shutdown the scheduler after 9 seconds, your program will also exit normally.

Can we cancel a callable task?

When we submit a task (Callable or its cousin Runnable) for execution to an Executor or ExecutorService (e.g. ThreadPoolExecutor), we get a Future back which wraps the tasks. It has a method called Future. cancel(...) which can be used to cancel the task the future wraps.

How do I stop a scheduler in Java?

The cancel() method is used to cancel the timer task. The cancel() methods returns true when the task is scheduled for one-time execution and has not executed until now and returns false when the task was scheduled for one-time execution and has been executed already.

What is a ScheduledExecutorService?

public interface ScheduledExecutorService extends ExecutorService. An ExecutorService that can schedule commands to run after a given delay, or to execute periodically. The schedule methods create tasks with various delays and return a task object that can be used to cancel or check execution.


1 Answers

The issue you have is that the scheduler keeps a live thread around after you have cancelled the beep task.

If there is a live non-daemon thread, the JVM stays alive.

The reason that it keeps this thread around is that you have told it to do so in this line:

private final ScheduledExecutorService scheduler         = Executors.newScheduledThreadPool(1); 

Note the documentation of newScheduledThreadPool(int corePoolSize):

corePoolSize - the number of threads to keep in the pool, even if they are idle.

So, you have two possible ways to cause the JVM to terminate:

  1. Pass 0 to newScheduledThreadPool instead of 1. The scheduler will not keep a live thread, and the JVM will terminate.

  2. Shut down the scheduler. You are supposed to do so anyway to release its resources. So change the run in your anonymous Runnable to:

    public void run() {     beeperHandle.cancel(true);     scheduler.shutdown(); } 

(In fact, you don't need the cancel there - the shutdown will take effect as soon as the next "beep" is completed.)

like image 97
RealSkeptic Avatar answered Sep 23 '22 01:09

RealSkeptic