1
我嘗試提交併在同一個流中獲得10 Future
s。每個人都需要1秒的時間來處理,我想平行運行它們。如何在相同的Java流中正確提交併獲取多個期貨?
我的第一次嘗試是takes_10_sec()
,它依次運行,需要10s。
我的第二次嘗試是takes_1_sec()
它並行運行並需要1秒。然而它使用中間的.collect(Collectors.toList()).stream()
,我認爲這不是一個好的方法。
還有另一種推薦的方法嗎?
public class FutureStream {
private ExecutorService executor = Executors.newFixedThreadPool(10);;
@Test
public void takes_10_sec() {
IntStream.range(0, 10)
.mapToObj(i -> longTask())
.map(task -> {
try {
return task.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
})
.forEach(System.out::println);
}
@Test
public void takes_1_sec() {
IntStream.range(0, 10)
.mapToObj(i -> longTask())
.collect(Collectors.toList())
.stream()
.map(task -> {
try {
return task.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
})
.forEach(System.out::println);
}
private Future<String> longTask() {
return executor.submit(() -> {
Thread.sleep(1000);
return Thread.currentThread().getName();
});
}
}