Açıklaması şöyle. Bu metod herhangi bir thread içinden çağrılabilir.
it can be completed from any thread that wants it to complete.Şöyle yaparız.
CompletableFuture<String> f = new CompletableFuture<>();
f.complete (...);Örnek
Elimizde şöyle bir kod olsun
public static void main(String[] args) {
  CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
    System.out.println("started work");
    LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(5));
    System.out.println("done work");
    return "a";
  });
  LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1));
  cf.complete("b");
  System.out.println(cf.join());
}Çıktı olarak şunu alırız. Burada çalışmakta olan CompletableFuture nesnesinin sonucunun - yani "a" değerinin" - gelmediği görülebilir.
started work
b
Hiç yorum yok:
Yorum Gönder