6

是否有一种内置方法可以取消以Runnable固定速率安排的任务,ScheduledExecutorService.scheduleAtFixedRate如果在调用取消时它恰好正在运行,则等待它的完成?

考虑以下示例:

public static void main(String[] args) throws InterruptedException, ExecutionException  {

    Runnable fiveSecondTask = new Runnable() {
        @Override
        public void run() {
            System.out.println("5 second task started");
            long finishTime = System.currentTimeMillis() + 5_000;
            while (System.currentTimeMillis() < finishTime);
            System.out.println("5 second task finished");
        }
    };

    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    ScheduledFuture<?> fut = exec.scheduleAtFixedRate(fiveSecondTask, 0, 1, TimeUnit.SECONDS);

    Thread.sleep(1_000);
    System.out.print("Cancelling task..");
    fut.cancel(true);

    System.out.println("done");
    System.out.println("isCancelled : " + fut.isCancelled());
    System.out.println("isDone      : " + fut.isDone());
    try {
        fut.get();
        System.out.println("get         : didn't throw exception");
    }
    catch (CancellationException e) {
        System.out.println("get         : threw exception");
    }
}

这个程序的输出是:

5 second task started
Cancelling task..done
isCancelled : true
isDone      : true
get         : threw exception
5 second task finished

设置一个共享的 volatile 标志似乎是最简单的选择,但如果可能的话,我宁愿避免它。

java.util.concurrent 框架是否内置了此功能?

4

1 回答 1

0

我不完全确定您要达到什么目的,但是当我从谷歌搜索到这里时,我认为这可能值得回答您的问题。

1)如果你想强行停止繁重的工作量 - 不幸的是,它似乎没有解决方案(当线程不响应中断时)。处理它的唯一方法是在循环中的耗时操作之间插入 Thread.sleep(1) ( http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html ) - 也许守护线程在这里会有所帮助,但我真的不鼓励使用它们。

2)如果您想阻止当前线程直到子线程完成,那么您可以使用 get http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html而不是调用取消#get()甚至超时。

3)如果你想彻底取消子线程,那么你可以调用:

fut.cancel(false);

这不会中断当前执行,但不会安排它再次运行。

4) 如果你的工作量不重,只需要等待 5 秒,那么使用线程睡眠或 TimeUnit 睡眠。在这种情况下,中断/取消将立即发生。

此外,您的示例缺少对 Executor 的关闭调用,这导致应用程序不会停止。

于 2015-04-02T22:47:43.963 回答