Future and FutureTask in Java allows you to write asynchronous code. Future is a general concurrency abstraction, also known as promise, which promises to return a result in future. In asynchronous programming, main thread doesn't wait for any task to finished, rather it hand over the task to workers and move on. One way of aynchronous processing is using callback methods. Future is another way to write asynchronous code. By using Future and FutureTask, you can write method which does long computation but return immediately. Those method, instead of returning result, return a Future object. You can later get result by calling Future.get() method, which will return object of type T, where T is what Future object is holding . One example of Future is submit() method of ExecutorService, which immediately return a Future object. By the way, Future and FutureTask are available in java.util.concurreent package from Java 1.5. Also, Future is and interface and FutureTask is an implementation or RunnableFuture, which can be used as Runnable interface, thus, can be passed to ExecutorService. In this Java concurrency tutorial, we will learn how to use Future and FutureTask in Java.
Future and FutureTask Example - Java
One of the simplest example of using Future is working with Thread pools. When you submit a long running task to ExecutorService, it returns a Future object immediately. This Future object can be used to query task completion and getting result of computation. In our sample Java program, we have a created a FactorialCalculator task, which wraps calculation of factorial under Callable interface's call() method. When we submit this task with job of calculating factorial of huger number like 100000, ExecutorService returns a Future object, which holds long value, return type of call method in our case. Later, we check whether task is completed or not using isDone() method. From output, you can see that main thread returns immediately. Since we have used get() method once task is completed, it doesn't block and return result immediately. By the way, Future object returned by submit() method is also an instance of FutureTask.
Important points Future and FutureTask in Java
1. Future is base interface and define abstraction of object which promises result to be available in future, while FutureTask is an implementation of Future interface.
2. Future is a parametric interface and type-safe written as Future, where V denotes value.
3. Future provides get() method to get result, which is blocking method and blocks until result is available to Future.
4. Future interface also defines cancel() method to cancel task.
5. isDone() and isCancelled() method is used to query Future task states. isDone() returns true if task is completed and result is available to Future. If you call get() method, after isDone() returned true then it should return immediately. On the other hand, isCancelled() method returns true, if this task is cancelled before its completion.
6. Future has four sub interfaces, each with additional functionality e.g. Response, RunnableFuture, RunnableScheduledFuture and ScheduledFuture. RunnableFuture also implements Runnable and successful finish of run() method cause completion of this Future.
7. FutureTask and SwingWorker are two well known implementation of Future interface. FutureTask also implements RunnableFuture interface, which means this can be used as Runnable and can be submitted to ExecutorService for execution.
8. Though most of the time ExecutorService creates FutureTask for you, i.e. when you submit() Callable or Runnable object. You can also created it manually.
9. FutureTask is normally used to wrap Runnable or Callable object and submit them to ExecutorService for asynchronous execution.
Future and FutureTask Example - Java
One of the simplest example of using Future is working with Thread pools. When you submit a long running task to ExecutorService, it returns a Future object immediately. This Future object can be used to query task completion and getting result of computation. In our sample Java program, we have a created a FactorialCalculator task, which wraps calculation of factorial under Callable interface's call() method. When we submit this task with job of calculating factorial of huger number like 100000, ExecutorService returns a Future object, which holds long value, return type of call method in our case. Later, we check whether task is completed or not using isDone() method. From output, you can see that main thread returns immediately. Since we have used get() method once task is completed, it doesn't block and return result immediately. By the way, Future object returned by submit() method is also an instance of FutureTask.
Important points Future and FutureTask in Java
1. Future is base interface and define abstraction of object which promises result to be available in future, while FutureTask is an implementation of Future interface.
2. Future is a parametric interface and type-safe written as Future
3. Future provides get() method to get result, which is blocking method and blocks until result is available to Future.
4. Future interface also defines cancel() method to cancel task.
5. isDone() and isCancelled() method is used to query Future task states. isDone() returns true if task is completed and result is available to Future. If you call get() method, after isDone() returned true then it should return immediately. On the other hand, isCancelled() method returns true, if this task is cancelled before its completion.
6. Future has four sub interfaces, each with additional functionality e.g. Response, RunnableFuture, RunnableScheduledFuture and ScheduledFuture. RunnableFuture also implements Runnable and successful finish of run() method cause completion of this Future.
7. FutureTask and SwingWorker are two well known implementation of Future interface. FutureTask also implements RunnableFuture interface, which means this can be used as Runnable and can be submitted to ExecutorService for execution.
8. Though most of the time ExecutorService creates FutureTask for you, i.e. when you submit() Callable or Runnable object. You can also created it manually.
9. FutureTask is normally used to wrap Runnable or Callable object and submit them to ExecutorService for asynchronous execution.
No comments:
Post a Comment