13
List<String> process() {
List<String> messages = Arrays.asList("Msg1", "Msg2", "Msg3", "Msg4", "Msg5", "Msg6", "Msg7", "Msg8", "Msg9",
"Msg10", "Msg11", "Msg12");
MessageService messageService = new MessageService();
ExecutorService executor = Executors.newFixedThreadPool(4);
List<String> mapResult = new ArrayList<>();
CompletableFuture<?>[] fanoutRequestList = new CompletableFuture[messages.size()];
int count = 0;
for (String msg : messages) {
CompletableFuture<?> future = CompletableFuture
.supplyAsync(() -> messageService.sendNotification(msg), executor).exceptionally(ex -> "Error")
.thenAccept(mapResult::add);
fanoutRequestList[count++] = future;
}
try {
CompletableFuture.allOf(fanoutRequestList).get();
//CompletableFuture.allOf(fanoutRequestList).join();
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mapResult.stream().filter(s -> !s.equalsIgnoreCase("Error")).collect(Collectors.toList());
}
Her iki yöntemleri ile denedim ama hiçbir fark içinde sonuç.
Teşekkürler
'almak()' gerektirir hem
get()
vejoin()
uygulanmasını bulabilirsiniz Yani bir try-catch bloğu gerekmez vedisscused
List<String> process
fonksiyonunu kullanırken yerine tamexceptionally()
yöntemini koşum kontrol edilen istisnaları yakalamak için. 'Get() 'ile' join() 'arasında bir değişiklik yaptığınızda fark etmelisiniz, zira derhal bir' 'InterruptedException' 'ya da' ExecutionException '' try' bloğuna atılmadığını söyleyen bir derleyici hatası alacaksınız. – Holger@ holi-java: 'join()' kesilemez. – Holger
@Holger evet, efendim. Görevi engelleyemedim. –