56.13 Handling a Future Result
The buttonClick() method is now configured to launch a Callable task with the return value assigned to the Future variable. The app now needs to know when the task is complete and the result available. One option is to call the get() method of the Future variable. Since this method is able to throw exceptions in the event that the execution fails or is interrupted, this must be performed in a try/catch statement as follows:
String result = null;
try {
result = future.get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
Unfortunately, the get() method will block the current thread until the task running in the thread completes, thereby defeating the purpose of running the task in a separate thread in the first place. Another option is to provide the get() method call with a timeout after which it will return control to the current thread. The following...