Admin service to start every x seconds

What is the correct way to make the executor service dispatch every x seconds?

public void start() {
    executorService.submit(new Runnable() {
        public void run(){
            ScheduledFuture<Integer> result = executorService.schedule(value, x, TimeUnit.SECONDS);
    });
}

      

Will wrapping the contents of the run method for some time (! Thread.currentThread (). IsInterrupted ()) be a valid approach - if not, how to do it?

thank

+3


source to share


1 answer


Use ScheduledExecutorService

:



ScheduledExecutorService ses = Executors.newScheduledThreadPool(10);
ses.scheduleAtFixedRate(new Runnable() {
    @Override
    public void run() {
        // do some work
    }
}, 0, x, TimeUnit.SECONDS);  // execute every x seconds

      

+9


source







All Articles