有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使ScheduledExecutorService在其计划任务取消时自动终止

如果网络连接已打开超过几个小时,我将使用ScheduledExecutorService关闭它。但是,在大多数情况下,网络连接在到达超时之前关闭,因此我取消ScheduledFuture。在本例中,我还希望executor服务终止并释放其线程池

令我惊讶的是,这并不是开箱即用的:虽然我在调度任务后调用了executor服务上的shutdown(),但executor服务不会在其唯一调度的任务被取消时自动终止。从ExecutorService.shutdown()的JavaDoc来看,这种行为甚至可能是正确的,因为可以说取消的任务尚未“执行”:

void java.util.concurrent.ExecutorService.shutdown()

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.

我的问题是,这是否可以更改:是否可以将executor服务配置为在其唯一的计划任务被取消时自动终止


或者与JUnit测试相同的问题:

@Test
public void testExecutorServiceTerminatesWhenScheduledTaskIsCanceled() throws Exception {
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

    Runnable task = new Runnable() {
        @Override
        public void run() {
            // ...
        }
    };

    ScheduledFuture<?> scheduledTask = scheduler.schedule(task, 2000, TimeUnit.MILLISECONDS);
    scheduler.shutdown();
    // do more configuration here...

    Thread.sleep(1000);

    scheduledTask.cancel(false);

    Thread.sleep(100);
    assertThat(scheduler.isTerminated(), is(true)); // ... so that this passes!?
}

共 (1) 个答案

  1. # 1 楼答案

    ScheduledThreadPoolExecutor(由Executors.newScheduledThreadPool返回的实际类型)文档中:

    When a submitted task is cancelled before it is run, execution is suppressed. By default, such a cancelled task is not automatically removed from the work queue until its delay elapses. While this enables further inspection and monitoring, it may also cause unbounded retention of cancelled tasks. To avoid this, set setRemoveOnCancelPolicy(boolean) to true, which causes tasks to be immediately removed from the work queue at time of cancellation.

    不过,这是一种ScheduledThreadPoolExecutor方法。我不相信纯粹使用ScheduledExecutorService接口就可以解决这个问题