简体   繁体   English

如果没有中断,Future.cancel()会做什么?

[英]What does Future.cancel() do if not interrupting?

From java docs on Future.cancel() 来自Future.cancel()上的java文档

boolean cancel(boolean mayInterruptIfRunning)

Attempts to cancel execution of this task. 尝试取消执行此任务。 This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason. 如果任务已完成,已取消或由于某些其他原因无法取消,则此尝试将失败。 If successful, and this task has not started when cancel is called, this task should never run. 如果成功,并且在调用cancel时此任务尚未启动,则此任务永远不会运行。 If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task. 如果任务已经启动, 则mayInterruptIfRunning参数确定执行此任务的线程是否应该在尝试停止任务时被中断

My question is what does cancel do if mayInterruptIfRunning is false? 我的问题是,如果mayInterruptIfRunning为false,取消会怎么做?
how does it cancel or stop the task from executing if it has already been run? 如果任务已经运行,它如何取消或停止执行?

If it is not interrupting it will simply tell the future that is is cancelled. 如果它没有打断它就会简单地告诉未来它被取消了。 You can check that via isCancelled() but nothing happens if you don't check that manually. 您可以通过isCancelled()检查,但如果不手动检查则没有任何反应。

Below example code shows how you could do it. 下面的示例代码显示了如何执行此操作。

private static FutureTask<String> task = new FutureTask<String>(new Callable<String>() {

    @Override
    public String call() throws Exception {
        while (!task.isCancelled()) {
            System.out.println("Running...");
            Thread.sleep(1000);
        }
        return "The End";
    }

});

public static void main(String[] args) throws InterruptedException {
    new Thread(task).start();
    Thread.sleep(1500);
    task.cancel(false);
}

The task is started, and after 1.5 iterations told to stop. 任务开始,并在1.5次迭代后告知停止。 It will continue sleeping (which it would not if you interrupted it) and then finish. 它将继续睡觉(如果你打断它就不会睡觉),然后完成。

My question is what does cancel do if mayInterruptIfRunning is false? 我的问题是,如果mayInterruptIfRunning为false,取消会怎么做? how does it cancel or stop the task from executing if it has already been run? 如果任务已经运行,它如何取消或停止执行?

If the task has already started running, and mayInterruptIfRunning is false , then there is nothing to be done. 如果任务已经开始运行,并且mayInterruptIfRunningfalse ,则无需执行任何操作。 In Java, interrupting a thread is considered to be the only safe way of stopping it short of completion - and even that requires the task to "comply" by checking for interruption at some implementation-specific interval. 在Java中,中断线程被认为是阻止它完成的唯一安全方法 - 甚至要求任务通过在特定实现的特定间隔检查中断来“遵守”。

Related: How do you kill a thread in Java? 相关: 如何在Java中杀死一个线程?

nothing will be done if the task has already started and mayInterruptIfRunning is false, 如果任务已经启动并且mayInterruptIfRunning为false,则不会执行任何操作,

below is the cancel() 下面是取消()

public boolean cancel(boolean mayInterruptIfRunning) {
    if (state != NEW)
        return false;
    if (mayInterruptIfRunning) {
        if (!UNSAFE.compareAndSwapInt(this, stateOffset, NEW, INTERRUPTING))
            return false;
        Thread t = runner;
        if (t != null)
            t.interrupt();
        UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED); // final state
    }
    else if (!UNSAFE.compareAndSwapInt(this, stateOffset, NEW, CANCELLED))
        return false;
    finishCompletion();
    return true;
}

we can see that if mayInterruptIfRunning is false,cancel() just change state from NEW to CANCELLED and return false,nothing else will be done 我们可以看到,如果mayInterruptIfRunning为false,则cancel()只是将状态从NEW更改为CANCELED并返回false,则不会执行任何其他操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM