本文介绍了未来取消方法文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 isDone 返回 true 如果取消(布尔值mayInterruptIfRunning)被调用。

According to http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html isDone returns true if cancel(boolean mayInterruptIfRunning) was called.

但是,任务可能正在运行且 mayInterruptIfRunning 设置为 false 。那么,在那次通话之后应该返回 isDone() true 因为取消(这是错误的)?

However, it is possible that task is running and mayInterruptIfRunning is set to false. So, what should return isDone() right after that call? true because of cancel (which is wrong)?

此外,还不清楚是否 cancel(boolean)方法返回 false

Also, it's not clear whether cancel(boolean) method returns false.

P. S.我正在实现一些简单的线程池,所以我继承自 Future

P. S. I'm implementing some simple thread pool, so I'm inheriting from Future.

推荐答案


  1. 取消(...)之后, isDone()应始终 true 。返回取消(...)并不重要。

  2. 如果取消(... )返回 true 这意味着此未来现已取消, isCancelled()== true

  3. 如果取消(...)返回 false 则表示完成不是由于此次调用 cancel()

  4. cancel(false)表示 cancel 方法不应该尝试取消试图完成未来的任务(任务的含义取决于未来的实现),任务将继续运行但未来被取消(isDone()== true)。

  5. 取消(true)表示应该尝试取消正在运行的任务,无论尝试是否成功,将来都会被取消(isDone()== true)。

  1. After cancel(...), isDone() should always be true. It doesn't matter what cancel(...) returned.
  2. If cancel(...) returns true it means this future is now cancelled and isCancelled()==true
  3. If cancel(...) returns false it means that the completion was not due to this call to cancel()
  4. cancel(false) means that the cancel method should not attempt to cancel the task that is trying to complete the future (the meaning of "task" depending on the Future's implementation), the task will keep running but the future is cancelled (isDone()==true).
  5. cancel(true) means that there should be an attempt to cancel the running task, regardless if the attempt is successful or not the future will be cancelled (isDone()==true).

请记住,这是一份合约,必须由 Future 的实施强制执行。

Remember that this is a contract, it has to be enforced by the Future's implementation.

编辑: isDone() cancel()之后始终为true / code>

isDone() is always true after cancel()

这是一个试验一些场景的测试:

Here's a test to experiment with some of the scenarios:

@Test
public void test() throws ExecutionException, InterruptedException {
    ExecutorService threadExecutor = Executors.newFixedThreadPool(1);
    CompletableFuture c1 = new CompletableFuture();
    CompletableFuture c2 = new CompletableFuture();
    Future<String> future = threadExecutor.submit(() -> {
        try {
            c1.complete(null);
            Thread.sleep(10000);
            c2.complete("normal");
        } catch (InterruptedException e) {
            c2.complete("interrupted");
        }
        return "aaa";
    });
    c1.join(); // waits for the task start
    // future.get(); // awaits the completion
    System.out.println("cancel:     " + future.cancel(true));
    //System.out.println("cancel:     " + future.cancel(false));
    System.out.println("isDone:     " + future.isDone());
    System.out.println("isCanceled: " + future.isCancelled());
    System.out.println("task:       " + c2.join());
}

这篇关于未来取消方法文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 20:35
查看更多