Executor Service Example
package com.nkondrat.learning;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ReusableThreadPoolExampleMain {
private final ExecutorService executorService;
public ReusableThreadPoolExampleMain(int threadPoolSize) {
this.executorService = Executors.newFixedThreadPool(threadPoolSize);
}
public void executeTasksAndWaitForCompletion(final List<Callable<String>> tasks)
throws InterruptedException {
final List<Future<String>> futures = executorService.invokeAll(tasks);
for (Future<String> future : futures) {
try {
final String result = future.get();
System.out.println("Result in for loop: " + result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void shutdown() {
executorService.shutdown();
}
static class ExampleTask implements Callable<String> {
private final int taskId;
public ExampleTask(int taskId) {
this.taskId = taskId;
}
@Override
public String call() throws Exception {
Thread.sleep(1000);
System.out.println("Executing task-" + taskId);
return String.format("Task-%d", taskId);
}
}
public static void main(String[] args) throws InterruptedException {
ReusableThreadPoolExampleMain threadPool =
new ReusableThreadPoolExampleMain(3);
List<Callable<String>> tasks = new ArrayList<>();
for (int i = 0; i < 10; i++) {
tasks.add(new ExampleTask(i));
}
threadPool.executeTasksAndWaitForCompletion(tasks);
}
}