賞金將在 2 天內到期。此問題的答案有資格獲得 100聲望賞金。 dcolazin希望引起對這個問題的更多關注。
這個問題與這個問題完全相同,實際上并沒有回答(該代碼只使用一個執行緒)。我的代碼現在看起來像這樣
CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> {
foo.stream().parallel()
.forEach(bar -> {
//business logic
}
);
return null;
}, new TraceableExecutorService(this.beanFactory, Executors.newFixedThreadPool(threads), "fooBarStream"));
completableFuture.get();
但只有一個執行緒被正確跟蹤。直接使用.parallelStream()or aLazyTraceExecutor代替 aTraceableExecutorService沒有幫助。
uj5u.com熱心網友回復:
由于這個例子,似乎作業。上面的片段變成:
TraceableExecutorService executorService = new TraceableExecutorService(this.beanFactory, Executors.newFixedThreadPool(threads), "fooStream");
CompletableFuture.allOf(runnablesBusinessLogic(foo,executorService)).get();
runnablesBusinessLogic在哪里
private CompletableFuture<Void>[] runnablesBusinessLogic(List<FooBar> foo, ExecutorService executorService) {
List<CompletableFuture<?>> futures = new ArrayList<>();
for (FooBar f : foo) {
futures.add(CompletableFuture.runAsync(() -> {
businessLogic(f);
return;
}, executorService));
}
return futures.toArray(new CompletableFuture[futures.size()]);
}
如果我正確理解了這個例子(以及檔案當前狀態背后的討論),Sleuth 就不能ForkJoinPool自動使用 a (以及并行流)。使其作業的主要思想不是創建一個CompletableFuture并拆分它,而是創建幾個期貨(并加入它們)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/453715.html
上一篇:useEffect中的異步處理
