"The future depends on what you do today" M. Gandhi
Future is java interface: public interface
Future<V>
A Future represents the result of an
asynchronous computation.
Methods of Future interface are provided to check
if the computation is complete, to wait for its completion, and to retrieve the
result of the computation. The class
that implements Future might look as followspublic class FutureImpl implements Future<String> { public boolean cancel(boolean mayInterruptIfRunning) { return false; } public boolean isCancelled() { return false; } public boolean isDone() { return false; } public String get() throws InterruptedException, ExecutionException { return null; } public String get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return null; } }
Methods of Future:
cancel() - attempts to cancel execution of
this task. This attempt will fail if the task has already completed, has
already been cancelled, or could not be cancelled for some other reason.
isDone() - Returns true if this task
completed. Completion may be due to normal termination, an exception, or
cancellation -- in all of these cases, this method will return true
get() - Waits if necessary for the
computation to complete, and then retrieves its result.
get(long timeout, TimeUnit unit) - Waits if
necessary for at most the given time for the computation to complete, and then
retrieves its result, if available
Thus, method ‘get’ is a key method: it
blocks current thread (with/without timeout) until the operation of another
thread is completed.
Mostly we don’t need to define custom
implementations of Future because Java library provides a bunch of various
built-in Future implementations. For example, FutureTask – it is a commonly
used implementation of the Future interface.
l FutureTask implements Runnable interface (so the FutureTask instance
could be called by Executor as well as be called directly).
l FutureTask may take Runnable as constructor parameter;
l Future task may take Callable as constructor parameter.
All these advantages mean that we have a
flexible and generic execution mechanism for our concurrent tasks, wrapping
Runnable/Callable instance with FutureTask we get the job which can be
scheduled, cancelled and the job result is available via Future methods ‘get’
There is some basic scenario of using
FutureTask
Callable callable = new MyCallable(); // initialize callable object ... FutureTask<String> futureTask = new FutureTask<String>(callable); // wrap the callable object with FutureTask ... ExecutorService executor = Executors.newFixedThreadPool(1); // initialize executor executor.execute(futureTask); // pass futureTask object to be executed ... // Then we can do the following while (true) { if (futureTask.isDone()) { System.out.println("Hooray, it's done"); executor.shutdown(); break; } else { System.out.println("Wait a moment"); } } // OR try { String s = futureTask.get(1000L, TimeUnit.MILLISECONDS); // wait a second for task completion, if it's not completed exception will occur System.out.println("Task result: " + s); } catch (ExecutionException e) { System.out.println("Too long task execution"); }
No comments:
Post a Comment