0

I need to implement a scheduled executor service which runs a thread in an interval every x seconds. The thread execution should be interrupted in case it took more than y seconds. I have tried to implement the solution using the ScheduledExecutorService that has a configurable parameter for the interval but does not have one for the timeout. I have a few ideas in mind and I would like to hear your suggestion for implementations/ techniques.

Itzik Shachar
  • 744
  • 5
  • 16
  • 3
    Does this help: http://stackoverflow.com/questions/30649643/scheduledexecutorservice-and-threadpooltaskexecutor-that-interrupts-tasks-after or that: http://stackoverflow.com/questions/2758612/executorservice-that-interrupts-tasks-after-a-timeout ... and hint: I simply put your question title into Google to get there ... that "prior research thing" is one powerful weapon, I tell you! – GhostCat Mar 22 '17 at 08:50
  • As already stated by GhostCat you should really use google first to get an idea on how you can solve your problem. When you have tried out something and it doesn't work as expected, feel free to post the code here and ask for help – JanTheGun Mar 22 '17 at 08:59
  • Thanks for the references. I decided to post the question here after doing a research. I already read the first one but for some reason, I skipped the second one. It might be what I was looking for. Thanks! – Itzik Shachar Mar 22 '17 at 09:01

1 Answers1

1

Does this help?Task begins every 10 seconds,and it takes 5 seconds to be done,you will get a InterruptedException when timedout(3 seconds).

import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Worker implements Runnable {
    ListeningExecutorService listeningExecutorService;
    ScheduledExecutorService scheduledExecutorService;
    Runnable task;

    public Worker(ListeningExecutorService listeningExecutorService, ScheduledExecutorService scheduledExecutorService, Runnable task) {
        this.listeningExecutorService = listeningExecutorService;
        this.scheduledExecutorService = scheduledExecutorService;
        this.task = task;
    }

    @Override
    public void run() {
        ListenableFuture future = listeningExecutorService.submit(task);
        Futures.withTimeout(future, 3, TimeUnit.SECONDS, scheduledExecutorService);
    }

    public static void main(String[] args) {
        ListeningExecutorService listeningExecutorService = MoreExecutors
            .listeningDecorator(Executors.newCachedThreadPool());
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
        Worker worker = new Worker(listeningExecutorService, scheduledExecutorService, new Runnable() {
            @Override
            public void run() {
                System.out.println("Now begin: " + new Date());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Now end: " + new Date());
            }
        });
        scheduledExecutorService.scheduleAtFixedRate(worker, 0, 10, TimeUnit.SECONDS);
    }
}