java.util.concurrent.Future中的方法cancel()是否会阻塞? [英] Whether method cancel() in java.util.concurrent.Future shoud be blocking?

查看:508
本文介绍了java.util.concurrent.Future中的方法cancel()是否会阻塞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的项目中实现Future<>界面。但看起来文档对它来说有点模糊。

I'm trying to implement Future<> interface in my project. But it looks like documentation is a little bit vague for it.

来自官方文档我们可以推断:


  1. 方法cancel()不会抛出InterruptedException或ExecutionException等异常。此外,它没有超时的变体。所以看起来,它不应该阻塞。

  2. 文档说

  1. Method cancel() does not throw exceptions like InterruptedException or ExecutionException. Moreover it has no the variant with timeout. So it looks like, it should NOT be blocking.
  2. Documentation says


之后此方法返回,后续调用isDone()
将始终返回true。

After this method returns, subsequent calls to isDone() will always return true.


boolean isDone()
如果此任务完成,则返回true。

boolean isDone() Returns true if this task completed.

因此,如果我们在任务处理时运行cancel()并且无法取消,则此方法应该等到任务完成。哪个与1相矛盾。

So if we run cancel() while the task is processing and cannot be canceled, this method should wait until the task is finished. Which contradicts with 1.

cancelite()的返回值描述为

The return value of cancel() described as


返回:如果无法取消任务,则返回false,通常是因为它已经正常完成;否则

Returns: false if the task could not be cancelled, typically because it has already completed normally; true otherwise

因此,如果任务正在运行且可能被取消但在此时刻没有取消,我们应该返回true(我们不能说它不能被取消)或等待(但它与1相矛盾)。

So, if the task is running and potentially can be cancelled but not at this exact moment, we should return true (we cannot state that it could not be cancelled) or wait (but it contradicts 1).

但是还有一个声明


如果此方法返回true,则对isCancelled()的后续调用将始终返回true。

Subsequent calls to isCancelled() will always return true if this method returned true.

但是


boolean isCancelled()
如果此任务在正常完成之前被取消,则返回true。

boolean isCancelled() Returns true if this task was cancelled before it completed normally.

当我们在任务运行时运行cancel()并且不能说任务是否可以取消时,这与3相矛盾(因为在这种情况下,cancel()应该返回true,但是isCancelled()应该返回false。

Which contradicts 3 in case when we run cancel() when the task is running and it cannot be said whether the task could be cancelled or not (because cancel() should return true in that case, but isCancelled() should return false).

它看起来像这样很久以前就已经开始使用API​​了,不应该出现这种不一致的情况在文档中。但那里有。我是否理解错误的内容?

It looks like this API have been delevoped long time ago and such inconsistences should not appear in the docs. But there are there. Do I understand something incorrectly?

推荐答案


我是否理解错误?

Do I understand something incorrectly?

我相信。 Future 不是作业控制API;它是对可能尚未计算的值概念的抽象。通过取消 Future ,您只需放弃对该值的兴趣;剩下的就是实现细节。

I believe so. Future is not a job control API; it is an abstraction over the concept of a value which may not yet have been computed. By cancelling a Future you simply waive your interest in that value; the rest is up to implementation detail.

因此, Future 与计算没有强烈的耦合最终产生结果。如果你调用取消并返回 true ,你已经移动了 Future 到达其最终的,不可更改的状态:取消 Future 的状态,它永远不会产生其值。基础计算任务可能会持续或不会持续不确定的时间;你通过 Future 的API 无法控制

As a consequence, Future is not strongly coupled to the computation which will eventually produce its result. If you invoke cancel and it returns true, you have moved the Future to its final, unchangeable state: that of a cancelled Future, which will never produce its value. The underlying computation task may or may not go on for an indeterminate amount of time; you have no control over that through the Future's API.

这篇关于java.util.concurrent.Future中的方法cancel()是否会阻塞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆