Future cancel method documentation

£可爱£侵袭症+ 提交于 2019-12-05 18:47:33
  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).

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

Edit: 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());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!