Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whether method cancel() in java.util.concurrent.Future should be blocking?

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

From official documentation we can deduce:

  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

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

    but

    boolean isDone() Returns true if this task completed.

    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.

  3. The return value of cancel() described as

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

    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).

  4. But there is also a statement

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

    but

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

    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).

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?

like image 987
Igor Lytvynenko Avatar asked Feb 24 '15 08:02

Igor Lytvynenko


People also ask

Does Future get block Java?

A Future interface provides methods to check if the computation is complete, to wait for its completion and to retrieve the results of the computation. The result is retrieved using Future's get() method when the computation has completed, and it blocks until it is completed.

What is Future cancel?

Cancelling a Future You can cancel a future using Future. cancel() method. It attempts to cancel the execution of the task and returns true if it is cancelled successfully, otherwise, it returns false.

What is Java Util Concurrent Future?

util. concurrent. Future , represents the result of an asynchronous computation. When the asynchronous task is created, a Java Future object is returned. This Future object functions as a handle to the result of the asynchronous task.

What is Future in Executor framework?

Two methods from the Future API will help us with this task. Future. isDone() tells us if the executor has finished processing the task. If the task is complete, it will return true; otherwise, it returns false. The method that returns the actual result from the calculation is Future.


1 Answers

Do I understand something incorrectly?

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.

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.

like image 198
Marko Topolnik Avatar answered Nov 14 '22 23:11

Marko Topolnik