29 Temmuz 2018 Pazar

CompletableFuture.runAsync metodu - Runnable Çalıştırır

runAsync() vs supplyAsync()
runAsync() metodları ile supplyAsync() metodları kardeş. Birisi girdi olarak Runnable alırken, diğeri Supplier alıyor. 

Aradaki fark şöyle. Her ikisi de CompletableFuture dönüyor, yani işin ne zaman bittiğini anlamak mümkün, ancak runAsync() Void döner.
CompletableFuture<Void> run = CompletableFuture.runAsync(()-> 
  System.out.println("hello"));

CompletableFuture<String> supply = CompletableFuture.supplyAsync(() -> {
  System.out.println("Hello");
  return "result";
});
System.out.println(supply.get());  //result

İşin sonucu get() ile almasak bile, runAsync hemen çalışır. 

Örnek
Elimizde şöyle bir kod olsun
// Case 2 - Don't use get()
CompletableFuture.runAsync(() -> {
  try {
    Thread.sleep(1_000L);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
  System.out.println("Hello");
});
System.out.println("World!");
Thread.sleep(5_000L); // For don't finish main thread
Çıktı olarak şunu alırız. Burada CompletableFuture'ın hemen çalıştığı görülebilir.
World!
Hello
runAsync metodu - Runnable
static Factory metodudur. Belirtilen işi ForkJoinPool içinde çalıştırır. Runnable sonuç dönmeyeceği için dönüş tipi void olur. Metodun imzası şöyle
CompletableFuture<Void> runAsync (Runnable runnable) 
Açıklaması şöyle.
By default (when no Executor is specified), asynchronous execution uses the common ForkJoinPool implementation, which uses daemon threads to execute the Runnable task. Note that this is specific to CompletableFuture. Other CompletionStage implementations can override the default behavior.
Örnek
Şöyle yaparız.
Queue<Supplier<CompletionStage<Void>>> queue = new ConcurrentLinkedQueue<>();

public void add(Runnable task) {
  queue.add(() -> CompletableFuture.runAsync(task));
}
Daha sonra işin bittiğini kontrol etmek için şöyle yaparız.
Supplier<CompletionStage<Void>> task = queue.poll();
try {
  task.get()
    .whenCompleteAsync((value, exception) -> ...)
    .exceptionally(exception -> ...);
} catch (Throwable exception) {
  ...
}
runAsync metodu - Runnable + Executor
static Factory metodudur. Belirtilen işi adanmış (dedicated) bir Executor içinde çalıştırır. Runnable sonuç dönmeyeceği için dönüş tipi void olur. 
Örnek
Şöyle yaparız
public void dbCall1() {
  ...
}

public void dbCall2() {
 ...
}

public void dbCall3() {
  ...
}

ExecutorService executor = Executors.newFixedThreadPool(16);
for (int i = 0; i < 5; i++) {
  CompletableFuture.runAsync(this::dbCall1, executor);
  CompletableFuture.runAsync(this::dbCall2, executor);
  CompletableFuture.runAsync(this::dbCall3, executor);
}

executor.shutdown();
executor.awaitTermination(2, TimeUnit.MINUTES);



Hiç yorum yok:

Yorum Gönder