Table of Contents
In this tutorial, we will see about Java FutureTask example.
FutureTask class has been introduced in JDK 5 with Executor Framework. FutureTask
class is the concrete implementation of the Future object and provides methods for start and cancel the task.It also provides method to see if the computation is done or not. We can query FutureTask object and get the result of computation.
If we call get method on FutureTask object, it is blocking call and returns once the computation is done.
Let’s understand more with the example.
Java FutureTask example:
Let’s create a very simple example.
Step 1: Create a Callable task named MultiplyingTask.java
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package org.arpit.java2blog; import java.util.concurrent.Callable; public class MultiplyingTask implements Callable{ int a; int b; long sleepTime; public MultiplyingTask(int a, int b, long sleepTime) { this.a=a; this.b=b; this.sleepTime=sleepTime; } @Override public Integer call() throws Exception { Thread.sleep(sleepTime); return a*b; } } |
Step 2: Create a class named FutureTaskMain
. This will be our main class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package org.arpit.java2blog; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.FutureTask; public class FutureTaskMain { public static void main(String[] args) { MultiplyingTask multiplyingTask1= new MultiplyingTask(10,20,2000l); MultiplyingTask multiplyingTask2= new MultiplyingTask(20,40,4000l); FutureTask<Integer> futureTask1=new FutureTask<>(multiplyingTask1); FutureTask<Integer> futureTask2=new FutureTask<>(multiplyingTask2); ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(futureTask1); executor.execute(futureTask2); while(true) { try { if(!futureTask1.isDone()) { System.out.println("FutureTask1 output="+futureTask1.get()); } if(!futureTask2.isDone()) { System.out.println("Waitng for futureTask2 for completion"); System.out.println("FutureTask2 output="+futureTask2.get()); } if(futureTask1.isDone() && futureTask2.isDone()) { System.out.println("Completed both the FutureTasks, shutting down the executors"); executor.shutdown(); return; } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } } } |
Let’s run above program to check the output:
Waitng for futureTask2 for completion
FutureTask2 output=800
Completed both the future task, shutting down the executors
Explanation:
- Create two callable task named multiplyingTask1 and multiplyingTask2. Please note that we have given sleep time as 2000 for multiplyingTask1 and 4000 for multiplyingTask2 so multiplyingTask2 will take more time than multiplyingTask1.
- Created two FutureTask objects named futureTask1 and futureTask2 by passing multiplyingTask1 and multiplyingTask2 respectively.
- Put an infinite loop condition with while(true)
- !futureTask1.isDone() checks for completion of futureTask1, if not done yet, we have called futureTask1.get(), as get method is blocking operation, current thread will wait for futureTask1 to complete.
- Once futureTask1 is done, we check !futureTask2.isDone() and above step is repeated for futureTask2.
- Once both the task i.e. futureTask1 and futureTask2 are done, we call shutdown() method on executors and return from it.
That’s all about Java FutureTask example.