1
我寫了一個簡單的例子來理解CompletableFuture。但是,當我在控制檯上打印它。有時它只是顯示「ASYN演示」 這是我的代碼使用java顯示錯誤的數據8 CompletableFuture
public class DemoAsyn extends Thread {
public static void main(String[] args) {
List<String> mailer = Arrays.asList("[email protected]", "[email protected]", "[email protected]", "[email protected]",
"[email protected]");
Supplier<List<String>> supplierMail =() -> mailer;
Consumer<List<String>> consumerMail = Mail::notifyMessage;
Function<List<String>,List<String>> funcMail = Mail::sendMessage;
CompletableFuture.supplyAsync(supplierMail).thenApply(funcMail).thenAccept(consumerMail);
System.out.println("asyn demo");
}
}
public class Mail {
public static List<String> sendMessage(List<String> notifies) {
notifies.forEach(x -> System.out.println("sent to " + x.toString()));
return notifies;
}
public static void notifyMessage(List<String> notifies) {
notifies.forEach(x -> System.out.println("notified to " + x.toString()));
}
}