Recently I am studying the concurrent concept on Java. And I wrote a demo code to help understand how `ExecutorService` works.
First, the code below is the worker code. It implements the `Callable` interface, and define a task to do. The simple task `MyTask` create is a task to print out a sentence on the console and return the `taskNum` assigned on initialization.
```java
import java.util.Random;
import java.util.concurrent.Callable;
public class MyTask implements Callable<Integer>{
private int taskNum;
public MyTask(int taskNum){
this.taskNum = taskNum;
}
@Override
public Integer call() throws Exception {
// TODO Auto-generated method stub
Random random = new Random();
System.out.println("This is `call()` method of Task #" + taskNum);
Thread.sleep(random.nextInt(500));
return taskNum;
}
}
```
We assign 30 tasks with different `taskNum` to the `ExecutorService` object and store the `Future` object, that is the result of our task, in an `ArrayList`. And finally, we try to get the result of all the tasks one by one.
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class ExecutorServiceDemo {
public static void main(String[] args) throws InterruptedException {
Random random = new Random();
List<Future<Integer>> results = new ArrayList<Future<Integer>>();
ExecutorService executor = Executors.newFixedThreadPool(2);
for (int i = 0; i < 30; i ++){
MyTask thisTask = new MyTask(i);
results.add(executor.submit(thisTask));
}
executor.shutdown();
System.out.println("Successfully distribute all the tasks!");
System.out.println("Waiting for the results!");
for (Future<Integer> result: results){
try {
System.out.println("Result: " + result.get(random.nextInt(500) , TimeUnit.MILLISECONDS));
} catch (Exception e) {
System.out.println("Result Timeout, drop result of this task");
}
}
}
}
```
Here is one of the possible result:
```text
This is `call()` method of Task #0
This is `call()` method of Task #1
Successfully distribute all the tasks!
Waiting for the results!
Result: 0
This is `call()` method of Task #2
Result Timeout, drop result of this task
Result: 2
This is `call()` method of Task #3
This is `call()` method of Task #4
Result: 3
This is `call()` method of Task #5
Result Timeout, drop result of this task
This is `call()` method of Task #6
Result Timeout, drop result of this task
This is `call()` method of Task #7
Result: 6
This is `call()` method of Task #8
Result: 7
This is `call()` method of Task #9
Result Timeout, drop result of this task
This is `call()` method of Task #10
Result Timeout, drop result of this task
This is `call()` method of Task #11
Result: 10
This is `call()` method of Task #12
Result Timeout, drop result of this task
Result: 12
This is `call()` method of Task #13
This is `call()` method of Task #14
Result: 13
This is `call()` method of Task #15
This is `call()` method of Task #16
Result: 14
Result: 15
This is `call()` method of Task #17
Result Timeout, drop result of this task
Result Timeout, drop result of this task
This is `call()` method of Task #18
Result: 18
This is `call()` method of Task #19
This is `call()` method of Task #20
This is `call()` method of Task #21
Result: 19
This is `call()` method of Task #22
Result: 20
Result Timeout, drop result of this task
Result: 22
This is `call()` method of Task #23
Result Timeout, drop result of this task
This is `call()` method of Task #24
This is `call()` method of Task #25
Result: 24
This is `call()` method of Task #26
This is `call()` method of Task #27
Result: 25
Result: 26
This is `call()` method of Task #28
Result: 27
This is `call()` method of Task #29
Result: 28
Result Timeout, drop result of this task
```