Skip to content

cryptoki/Future_Dojo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Future Dojo

Wiederholung vom 27. Februar 2015

Allgemein

  • supplyAsync() - Konstruiert ein neues CompletableFuture Objekt
    • benötigt Supplier und optional Executor
    • Executors.newFixedThreadPool(5)
CompleteableFuture.supplyAsyn( () -> doSomethingAndReturnResult() );
  • runAsync
CompleteableFuture<?> future = CompleteableFuture.runAsync(
  () -> System.out.println("Dies ist super cool!");
);
  • thenApply() - Führt eine Aktion in Form eines Function<T,R> aus
  • thenAccept() - Führt eine abschließende Aktion aus

Aufrufe erfolgen im aktuellen Thread (blockierend)

  • es gibt zu jeder Methode eine async Variante, endet mit Async

Dojo

complete()


  • mit complete wird das Ergebnis einer "noch laufenden" Berechnung von außen vorzeitig gesetzt. Das Berechnungsergebnis wird ignoriert
CompleteableFuture<Integer> future = CompleteableFuture.supplyAsync(
  () -> {
    try { 
      Thread.sleep(5000); return 1234; 
    } catch (Exception e) {}
  });
future.complete(1);
  • man kann das ganze auch nutzen, um etwas anzutriggern

new()


CompleteableFuture<Integer> future = new CompleteableFuture<Integer>();
future.complete(1);
  • erzeugt ein CompleteableFuture das über complete mit einem Wert versorgt werden kann

async()


  • Methoden mit dem Namens-Postfix ...Async führen ihre Berechnung in einem Thread des expliziten oder impliziten Executors aus
  • Methoden ohne den Namens-Postfix ...Async führen ihre Berechnung im Thread des Aufrufers der Methode aus

Exceptions


  • eine geprüfte Exception darf NICHT geworfen werden
  • ungeprüfte dürfen geworfen werden und können mit exceptionally abgefangen werden

Abbruch


  • complete .. normales Ende
  • completeExceptionally .. Ende mit einer Exception

Beispiele

Example 1
  [Supplier] creating something new cool stuff    (Thread[ForkJoinPool.commonPool-worker-1,5,main])
  [Supplier] SupplyNewResult -sleep-    (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - ...
starting example1 Thread[main,5,main]
done example1 Thread[main,5,main]
  [Supplier] SupplyNewResult +done+     (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - ...
Process finished with exit code 0
Example 2
  • Abarbeitung erfolgt im aktuellen Thread, d.h. sequentiell
  [Supplier] creating something new cool stuff    (Thread[ForkJoinPool.commonPool-worker-1,5,main])
  [Supplier] SupplyNewResult -sleep-    (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - ...
starting example2 Thread[main,5,main]
done example2 Thread[main,5,main]
  [Supplier] SupplyNewResult +done+     (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - ...
  [1] SetCountAndGetAsFuture     (Thread[ForkJoinPool.commonPool-worker-2,5,main])
  [1] SetCountAndGetAsFuture -sleep-    (Thread[ForkJoinPool.commonPool-worker-2,5,main]) - ...
  [1] SetCountAndGetAsFuture +done+     (Thread[ForkJoinPool.commonPool-worker-2,5,main]) - ...
  [2] SetInfoAndGetAsFuture      (Thread[ForkJoinPool.commonPool-worker-2,5,main])
  [2] SetInfoAndGetAsFuture  -sleep-    (Thread[ForkJoinPool.commonPool-worker-2,5,main]) - ...
  [2] SetInfoAndGetAsFuture  +done+     (Thread[ForkJoinPool.commonPool-worker-2,5,main]) - ...
  [Consume] Result with values Result{info='magic', count=1, nonBlocking=null, async=null, error=null}    (Thread[ForkJoinPool.commonPool-worker-2,5,main]).
ready example2 Thread[main,5,main]

Process finished with exit code 0
Example 3
  [Supplier] creating something new cool stuff    (Thread[ForkJoinPool.commonPool-worker-1,5,main])
  [Supplier] SupplyNewResult -sleep-    (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - ...
starting example3 Thread[main,5,main]
done example3 Thread[main,5,main]
  [Supplier] SupplyNewResult +done+     (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - ...
  [1] SetCountAndGetAsFuture     (Thread[ForkJoinPool.commonPool-worker-2,5,main])
  [1] SetCountAndGetAsFuture -sleep-    (Thread[ForkJoinPool.commonPool-worker-2,5,main]) - ...
  [1] SetCountAndGetAsFuture +done+     (Thread[ForkJoinPool.commonPool-worker-2,5,main]) - ...
  [2] SetInfoAndGetAsFuture      (Thread[ForkJoinPool.commonPool-worker-2,5,main])
  [2] SetInfoAndGetAsFuture  -sleep-    (Thread[ForkJoinPool.commonPool-worker-2,5,main]) - ...
  [2] SetInfoAndGetAsFuture  +done+     (Thread[ForkJoinPool.commonPool-worker-2,5,main]) - ...
ready example3 Thread[main,5,main]

Process finished with exit code 0
Example 4
  [Supplier] creating something new cool stuff    (Thread[ForkJoinPool.commonPool-worker-1,5,main])
  [Supplier] SupplyNewResult -sleep-    (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
starting example4 Thread[main,5,main]
done example4 Thread[main,5,main]
  [Supplier] SupplyNewResult +done+     (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
  [2] SetInfoAndGetAsFuture      (Thread[ForkJoinPool.commonPool-worker-2,5,main])
  [1] SetCountAndGetAsFuture     (Thread[ForkJoinPool.commonPool-worker-3,5,main])
ready example4 Thread[main,5,main]
  [1] SetCountAndGetAsFuture -sleep-    (Thread[ForkJoinPool.commonPool-worker-3,5,main]) - .
  [2] SetInfoAndGetAsFuture  -sleep-    (Thread[ForkJoinPool.commonPool-worker-2,5,main]) - .

Process finished with exit code 0
Example 5
  [Supplier] creating something new cool stuff    (Thread[ForkJoinPool.commonPool-worker-1,5,main])
  [Supplier] SupplyNewResult -sleep-    (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
starting example5 Thread[main,5,main]
done example5 Thread[main,5,main]
  [Supplier] SupplyNewResult +done+     (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
  [2] SetInfoAndGetAsFuture      (Thread[ForkJoinPool.commonPool-worker-2,5,main])
  [1] SetCountAndGetAsFuture     (Thread[ForkJoinPool.commonPool-worker-1,5,main])
  [2] SetInfoAndGetAsFuture  -sleep-    (Thread[ForkJoinPool.commonPool-worker-2,5,main]) - .
  [1] SetCountAndGetAsFuture -sleep-    (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
  [2] SetInfoAndGetAsFuture  +done+     (Thread[ForkJoinPool.commonPool-worker-2,5,main]) - .
  [1] SetCountAndGetAsFuture +done+     (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
  [Consume] Result with values Result{info='magic', count=1, nonBlocking=null, async=null, error=null}    (Thread[main,5,main]).
ready example5 Thread[main,5,main]

Process finished with exit code 0
Example 6
  [Supplier] creating something new cool stuff    (Thread[ForkJoinPool.commonPool-worker-1,5,main])
  [Supplier] SupplyNewResult -sleep-    (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
starting example6 Thread[main,5,main]
done example6 Thread[main,5,main]
  [Supplier] SupplyNewResult +done+     (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
  [2] SetInfoAndGetAsFuture      (Thread[ForkJoinPool.commonPool-worker-2,5,main])
  [2] SetInfoAndGetAsFuture  -sleep-    (Thread[ForkJoinPool.commonPool-worker-2,5,main]) - .
  [1] SetCountAndGetAsFuture     (Thread[ForkJoinPool.commonPool-worker-1,5,main])
  [1] SetCountAndGetAsFuture -sleep-    (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
  [2] SetInfoAndGetAsFuture  +done+     (Thread[ForkJoinPool.commonPool-worker-2,5,main]) - .
  [1] SetCountAndGetAsFuture +done+     (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
lets combine together Thread[ForkJoinPool.commonPool-worker-1,5,main]
bla Result{info='magic', count=1, nonBlocking=null, async=null, error=null}
  [Consume] Result with values Result{info='magic', count=1, nonBlocking=null, async=null, error=null}    (Thread[ForkJoinPool.commonPool-worker-1,5,main]).
blub Result{info='magic', count=1, nonBlocking=null, async=null, error=null}
ready example6 Thread[main,5,main]

Process finished with exit code 0
Example 7
  [Supplier] creating something new cool stuff    (Thread[ForkJoinPool.commonPool-worker-1,5,main])
  [Supplier] SupplyNewResult -sleep-    (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
starting example7 Thread[main,5,main]
done example7 Thread[main,5,main]
  [Supplier] SupplyNewResult +done+     (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
  [2] SetInfoAndGetAsResult      (Thread[ForkJoinPool.commonPool-worker-1,5,main])
  [2] SetInfoAndGetAsResult  -sleep-    (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
  [2] SetInfoAndGetAsResult  +done+     (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
  [1] SetCountAndGetAsResult     (Thread[ForkJoinPool.commonPool-worker-1,5,main])
  [1] SetCountAndGetAsResult -sleep-    (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
  [1] SetCountAndGetAsResult +done+     (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
lets combine together Thread[ForkJoinPool.commonPool-worker-1,5,main]
ready example7 Thread[main,5,main]
  [Consume] Result with values Result{info='magic', count=1, nonBlocking=null, async=null, error=null}    (Thread[ForkJoinPool.commonPool-worker-1,5,main]).

Process finished with exit code 0
Example 8
  [Supplier] creating something new cool stuff    (Thread[ForkJoinPool.commonPool-worker-1,5,main])
  [Supplier] SupplyNewResult -sleep-    (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
starting example8 Thread[main,5,main]
done example8 Thread[main,5,main]
  [Supplier] SupplyNewResult +done+     (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
  [1] SetCountAndGetAsResult     (Thread[ForkJoinPool.commonPool-worker-1,5,main])
  [2] SetInfoAndGetAsResult      (Thread[ForkJoinPool.commonPool-worker-2,5,main])
  [1] SetCountAndGetAsResult -sleep-    (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
  [2] SetInfoAndGetAsResult  -sleep-    (Thread[ForkJoinPool.commonPool-worker-2,5,main]) - .
  [2] SetInfoAndGetAsResult  +done+     (Thread[ForkJoinPool.commonPool-worker-2,5,main]) - .
  [1] SetCountAndGetAsResult +done+     (Thread[ForkJoinPool.commonPool-worker-1,5,main]) - .
lets combine together Thread[ForkJoinPool.commonPool-worker-1,5,main]
  [Consume] Result with values Result{info='magic', count=1, nonBlocking=null, async=null, error=null}    (Thread[ForkJoinPool.commonPool-worker-1,5,main]).
ready example8 Thread[main,5,main]

Process finished with exit code 0
Example 9

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages