Пример ForkJoinPool по умолчанию
Код: Выделить всё
List lists = new ArrayList();
for (int i = 0; i < 10; i++) {
CompletableFuture future = CompletableFuture.supplyAsync(() -> {
List list = new ArrayList(1000);
for (int j = 0; j < 1000; j++) {
list.add("test" + j);
System.out.println(Thread.currentThread().getName() + " " + j);
}
return list;
});
lists.add(future);
}
CompletableFuture allFuture = CompletableFuture.allOf(lists.toArray(new CompletableFuture[0]));
allFuture.thenRun(() -> {
System.out.println("done");
});
Код: Выделить всё
int threadNum = Runtime.getRuntime().availableProcessors() * 2;
ExecutorService executorService = new ThreadPoolExecutor(threadNum, threadNum, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue(10), new ThreadPoolExecutor.CallerRunsPolicy());
List lists = new ArrayList();
for (int i = 0; i < 10; i++) {
CompletableFuture future = CompletableFuture.supplyAsync(() -> {
List list = new ArrayList(1000);
for (int j = 0; j < 1000; j++) {
list.add("test" + j);
System.out.println(Thread.currentThread().getName() + " " + j);
}
return list;
},executorService);
lists.add(future);
}
CompletableFuture allFuture = CompletableFuture.allOf(lists.toArray(new CompletableFuture[0]));
allFuture.thenRun(() -> {
System.out.println("done");
});
Подробнее здесь: https://stackoverflow.com/questions/791 ... te-running
Мобильная версия