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

查看:148
本文介绍了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.

但是


布尔值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.

cancel()的返回值描述为

The return value of cancel() described as


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

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.

但是


布尔值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?

我相信是这样。 未来不是工作控制API;它是对可能尚未计算的值的概念的抽象。通过取消期货,您只需放弃对该价值的兴趣;

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 的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天全站免登陆