Executor executor = Executors.newFixedThreadPool(4);
Runnable task = () -> {
System.out.println("Задача начала выполняться в потоке: " +
Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Это задача, выполняемая в ином потоке, отличном от потока main: " +
Thread.currentThread().getName());
};
List<CompletableFuture<Void>> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(runAsync(task, executor));
}
list.forEach(x -> {
try {
x.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
});