监听提交到线程池的任务是否超时

Monitors whether the task submitted to the thread pool timed out

我有一个会一直调用的方法。

调用后会生成一个job(runnable)提交给线程池。每个job的超时时间不同,取决于传入的参数。

现在想监控每个job在开始执行时是否能在超时时间内结束。我该怎么办?

注意timeout是从开始执行到执行结束,不是从投递到线程池到任务执行结束。因此,我认为 future #get (timeout) 不能使用,对吗?

并且acceptJob不应该阻塞,它必须在提交作业后立即return(可能是其他逻辑,但不会阻塞)。

ExecutorService pool = Executors.newFixedThreadPool(10);

public void acceptNewJob(Map<String, Object> params) {
    // timeout from params
    int timeoutInMs = (int) params.get("timeoutInMs");
    pool.submit(new Runnable() {
        @Override
        public void run() {
            // generate a job by params
            // if this job execute timeout, need alarm
        }
    });
}

如何包装每个 runnable 并使用 Timer 在超时期限到期时检查 runnable 的状态。

    public void acceptNewJob(Map<String, Object> params) {
        // timeout from params
        int timeoutInMs = (int) params.get("timeoutInMs");
        MonitoredRunnable runnable = new MonitoredRunnable(new Runnable() {
            @Override
            public void run() {
                // generate a job by params
                // if this job execute timeout, need alarm
            }
        }, timeoutInMs);
        pool.submit(runnable);
    }

    // Or use ScheduledThreadPoolExecutor
    private Timer timer = new Timer();

    public class MonitoredRunnable implements Runnable {
        private volatile int state = READY;

        public static final int READY = 0;
        public static final int RUNNING = 1;
        public static final int COMPLETE = 0;

        private Runnable task;
        private int timeoutInMs;

        public MonitoredRunnable(Runnable task, int timeoutInMs) {
            this.task = task;
            this.timeoutInMs = timeoutInMs;
        }

        @Override
        public void run() {
            state = RUNNING;
            startMonitor(this);
            task.run();
            state = COMPLETE;
        }

        private void startMonitor(MonitoredRunnable runnable) {
            timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    if (runnable.state != COMPLETE) {
                        System.out.println("Job timeout.");
                        // alarm
                    }
                } catch (Exception e) {
                    //
                }
            }
        }, runnable.timeoutInMs);
    }