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?



source to share

1 answer

Use ScheduledExecutorService


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




All Articles