Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asinhrone budućnosti i verteksi

Asinhrone budućnosti i verteksi

DaFED#43
Speaker: Igor Spasić
Osvrt na principe asinhronog programiranja. Šta Java8 donosi novo povodom toga. Predstavljanje Vert.x-a (vertx.io).

DaFED

May 06, 2016
Tweet

More Decks by DaFED

Other Decks in Programming

Transcript

  1. 4 fun fact #1 4 cool fact #2 4 i

    ovde kao još nešto... DAFEDLOVE | HeapSpace, 2016 3
  2. Sync IO 4 Dolazi input 4 CPU čeka da se

    I završi 4 Procesiranje 4 CPU čeka da se O završi DAFEDLOVE | HeapSpace, 2016 8
  3. Multi Threading 4 (Svaki) ulaz ima jedan thread 4 CPU

    switch threadova dok su oni blokirani na I. 4 Kada CPU izabere thread koji je spreman, započinje obradu DAFEDLOVE | HeapSpace, 2016 11
  4. ExecutorService executorService = Executors.newFixedThreadPool(10); executorService.execute(new Runnable() { public void run()

    { System.out.println("Asynchronous task"); } }); executorService.shutdown(); DAFEDLOVE | HeapSpace, 2016 15
  5. synchronized? 4 Jedan monitor po objektu 4 Nema načina da

    se prekine čekanje za lock 4 Nema time-outa 4 Blok struktura koda 4 Teško je ostvariti multiple lock 4 Nema naprednih lokovanja DAFEDLOVE | HeapSpace, 2016 21
  6. ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readWriteLock.readLock().lock(); // multiple readers, if

    not writing readWriteLock.readLock().unlock(); readWriteLock.writeLock().lock(); // one writer, if not reading readWriteLock.writeLock().unlock(); DAFEDLOVE | HeapSpace, 2016 23
  7. Future future = executorService.submit( new Callable() { public Object call()

    throws Exception { System.out.println("Asynchronous Callable"); return "Callable Result"; } } ); // not blocked, do something! future.get(); // block DAFEDLOVE | HeapSpace, 2016 43
  8. CompletableFuture 4 Functional 4 Monadic (chain!) 4 Asynchronous 4 Event-driven

    4 Bogat API 4 Tri varijante metoda DAFEDLOVE | HeapSpace, 2016 48
  9. CompletableFuture 4 chaining (1 - 1) 4 composition (1 -

    1) 4 kombinacija, čekanje na rezultat (2 - 1) 4 kombinacija, javi se posle prvog (2 - 1) 36 metoda!!! DAFEDLOVE | HeapSpace, 2016 49
  10. CompletableFuture 4 u istom executoru kao caller 4 u novom

    Executor 4 u common fork-join pool-u 36 metoda!!! DAFEDLOVE | HeapSpace, 2016 50
  11. Kreiraj supplyAsync, runAsync CompletableFuture<Integer> f = supplyAsync( () -> {sleep(1000);

    return 42;} ); // do something f.get(); // f.join(); DAFEDLOVE | HeapSpace, 2016 51
  12. Kombinacija thenCombine CompletableFuture<Integer> f1 = supplyAsync( () -> {sleep(1000); return

    42;} ); CompletableFuture<Integer> f2 = supplyAsync( () -> {sleep(1000); return 52;} ); f3 = f1.thenCombine(f2, (r1, r2) -> r1 + r2)); DAFEDLOVE | HeapSpace, 2016 54
  13. Primer CompletableFuture.supplyAsync( () -> readPage("http://jodd.org/") ) .thenApply( page -> linkParser.getLinks(page))

    .thenAcceptAsync( links -> displayPanel.display(links) ); DAFEDLOVE | HeapSpace, 2016 55
  14. Poslednji primer CompletableFuture<String> closing = new CompletableFuture<>(); Stream<String> manyStrings =

    Stream.of("one", "two", "three"); CompletableFuture<String> reduce = manyStrings .parallel() // ! .onClose(() -> closing.complete(">")) .filter(s -> s.length() < 4) .map(CompletableFuture::completedFuture) .reduce(closing, (cf1, cf2) -> cf1.thenCombine( cf2, (BinaryOperator<String>) (s1, s2) -> s1 + s2) ); manyStrings.close(); System.out.println(reduce.get()); DAFEDLOVE | HeapSpace, 2016 57
  15. Multi Threading 4 (Svaki) ulaz ima jedan thread 4 CPU

    switch threadova dok su oni blokirani na I. 4 Kada CPU izabere thread koji je spreman, započinje obradu DAFEDLOVE | HeapSpace, 2016 60
  16. Async 'Blocking' IO 4 I se skenira periodično dok se

    ne nađe spreman 4 # tredova << # inputa 4 manji context switch 4 select(), poll() DAFEDLOVE | HeapSpace, 2016 62
  17. Async Non-Blocking IO 4 I obaveštava CPU kada je spreman

    4 Potreban je samo 1 thread / CPU 4 ne postoji context switch 4 epoll() DAFEDLOVE | HeapSpace, 2016 63
  18. Async, simplified 1. Make system call to selector 2. if

    nothing to do, `goto 1` 3. loop through tasks a. if its an `OP_ACCEPT`, system call to accept the connection, save the key b. if its an `OP_READ`, find the key, system call to read the data c. if more tasks `goto 3` 4. `goto 1` DAFEDLOVE | HeapSpace, 2016 65
  19. Server.java public class Server extends Verticle { public void start()

    { vertx.createHttpServer().requestHandler( new Handler<HttpServerRequest>() { public void handle(HttpServerRequest req) { String file = req.path().equals("/") ? "index.html" : req.path(); req.response().sendFile("webroot/" + file); } }).listen(8080); } } DAFEDLOVE | HeapSpace, 2016 68
  20. Server.java public class Server extends Verticle { public void start()

    { vertx.createHttpServer().requestHandler( req -> { String file = req.path().equals("/") ? "index.html" : req.path(); req.response().sendFile("webroot/" + file); } ).listen(8080); } } DAFEDLOVE | HeapSpace, 2016 69