Reputation: 7524
From Java doc:
A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation.
How is then it asynchronous if there are methods to wait for completion?My understanding of asynchronous operation is that caller can just make a call to it, and just move to some other task. And caller would come to know of the completion automatically, with result. Is this wrong?
Upvotes: 0
Views: 5664
Reputation: 13535
There are asynchronous computations and asynchronous interactions. Usually asynchronous computations use asynchronous interactions, and synchronous computations use synchronous interactions. Synchronous computations are threads, and synchronous interactions are blocking queues, semaphores etc.
But synchronous and asynchronous worlds need to interact, and here Future
comes out: it provides result of an asynchronous operation to a synchronous computation (thread).
Upvotes: 0
Reputation: 340158
My understanding of asynchronous operation is that caller can just make a call to it, and just move to some other task.
See the definition of asynchronous operation. The term refers to timing, not about techniques for coordination.
The task to be done by in the background by the other thread happens at any time. Coordinating with the originating thread is an unrelated issue, neither required nor denied by the “async” term. So yes, the originating thread/object is free to move on to doing other work while the background thread is performing the delegated task. The originating thread/object may or may not be informed of the task’s completion.
And caller would come to know of the completion automatically, with result. Is this wrong?
Yes, that is wrong. The delegated task may have nothing to do with the originating thread/object. The originating thread/object may have no interest in the task’s completion, and if that is the case, certainly would not want to be interrupted in any way.
Even if the originating thread does care about completion of the delegated task, being asynchronous by definition does not define a vehicle by which the originating thread will be notified. In the old days of C-style coding, a call-back function was commonly defined. In OOP there are various techniques by which the originating object may be notified. One of these techniques is for the originating object to check the task’s status by interrogating a Future
.
This is demonstrated in the example code shown in the Future
class doc:
interface ArchiveSearcher { String search(String target); }
class App {
ExecutorService executor = ...
ArchiveSearcher searcher = ...
void showSearch(String target) throws InterruptedException {
Callable<String> task = () -> searcher.search(target);
Future<String> future = executor.submit(task);
displayOtherThings(); // do other things while searching
try {
displayText(future.get()); // use future
} catch (ExecutionException ex) { cleanup(); return; }
}
}
Note that Java 8 brought new and useful kinds of Future
implementations.
Upvotes: 4