Pro Yearly is on sale from $80 to $50! »

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).

C1d9295b73069d0182d61895048713d8?s=128

DaFED

May 06, 2016
Tweet

Transcript

  1. DAFEDLOVE | HeapSpace, 2016 1

  2. Async java DAFEDLOVE | HeapSpace, 2016 2

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

    ovde kao još nešto... DAFEDLOVE | HeapSpace, 2016 3
  4. Šta svaki program radi? ulaz, obrada, izlaz DAFEDLOVE | HeapSpace,

    2016 4
  5. Šta svaki program koristi? Memoriju i CPU DAFEDLOVE | HeapSpace,

    2016 5
  6. CPU može da radi samo JEDNU stvar DAFEDLOVE | HeapSpace,

    2016 6
  7. Ups. DAFEDLOVE | HeapSpace, 2016 7

  8. 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
  9. Pravilo CPU ne sme da čeka! DAFEDLOVE | HeapSpace, 2016

    9
  10. Rešenje Ako CPU čeka IO neka switch context DAFEDLOVE |

    HeapSpace, 2016 10
  11. 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
  12. DAFEDLOVE | HeapSpace, 2016 12

  13. Gde je tu Java? IO/NIO/NIO2/... DAFEDLOVE | HeapSpace, 2016 13

  14. Async Java Executors DAFEDLOVE | HeapSpace, 2016 14

  15. ExecutorService executorService = Executors.newFixedThreadPool(10); executorService.execute(new Runnable() { public void run()

    { System.out.println("Asynchronous task"); } }); executorService.shutdown(); DAFEDLOVE | HeapSpace, 2016 15
  16. ExecutorService executorService = Executors.newFixedThreadPool(10); executorService.execute(() -> System.out.println("Asynchronous task") }); executorService.shutdown();

    DAFEDLOVE | HeapSpace, 2016 16
  17. Pravilo Zaključaj deljene resurse DAFEDLOVE | HeapSpace, 2016 17

  18. DAFEDLOVE | HeapSpace, 2016 18

  19. DAFEDLOVE | HeapSpace, 2016 19

  20. Async Java Lock DAFEDLOVE | HeapSpace, 2016 20

  21. 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
  22. Lock lock = new ReentrantLock(); lock.lock(); // critical section lock.unlock();

    DAFEDLOVE | HeapSpace, 2016 22
  23. 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
  24. Izazov Multithreading NIJE rešenje DAFEDLOVE | HeapSpace, 2016 24

  25. DAFEDLOVE | HeapSpace, 2016 25

  26. Scenario 2 threads 3 inputs DAFEDLOVE | HeapSpace, 2016 26

  27. DAFEDLOVE | HeapSpace, 2016 27

  28. DAFEDLOVE | HeapSpace, 2016 28

  29. DAFEDLOVE | HeapSpace, 2016 29

  30. DAFEDLOVE | HeapSpace, 2016 30

  31. DAFEDLOVE | HeapSpace, 2016 31

  32. DAFEDLOVE | HeapSpace, 2016 32

  33. DAFEDLOVE | HeapSpace, 2016 33

  34. DAFEDLOVE | HeapSpace, 2016 34

  35. DAFEDLOVE | HeapSpace, 2016 35

  36. DAFEDLOVE | HeapSpace, 2016 36

  37. DAFEDLOVE | HeapSpace, 2016 37

  38. Multithreading != Concurrent DAFEDLOVE | HeapSpace, 2016 38

  39. Rešenje? Async! DAFEDLOVE | HeapSpace, 2016 39

  40. DAFEDLOVE | HeapSpace, 2016 40

  41. DAFEDLOVE | HeapSpace, 2016 41

  42. Async Java Future DAFEDLOVE | HeapSpace, 2016 42

  43. 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
  44. Nažalost... DAFEDLOVE | HeapSpace, 2016 44

  45. DAFEDLOVE | HeapSpace, 2016 45

  46. Your Future sucks! Callback Hell DAFEDLOVE | HeapSpace, 2016 46

  47. Async Java 8 CompletableFuture (aka Promise) DAFEDLOVE | HeapSpace, 2016

    47
  48. CompletableFuture 4 Functional 4 Monadic (chain!) 4 Asynchronous 4 Event-driven

    4 Bogat API 4 Tri varijante metoda DAFEDLOVE | HeapSpace, 2016 48
  49. 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
  50. CompletableFuture 4 u istom executoru kao caller 4 u novom

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

    return 42;} ); // do something f.get(); // f.join(); DAFEDLOVE | HeapSpace, 2016 51
  52. Nastavi akciju posle thenApply CompletableFuture<Integer> f2 = f.thenApply((r) -> r*r);

    DAFEDLOVE | HeapSpace, 2016 52
  53. Greške exceptionally, handle f.exceptionally((t) -> 2); DAFEDLOVE | HeapSpace, 2016

    53
  54. 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
  55. Primer CompletableFuture.supplyAsync( () -> readPage("http://jodd.org/") ) .thenApply( page -> linkParser.getLinks(page))

    .thenAcceptAsync( links -> displayPanel.display(links) ); DAFEDLOVE | HeapSpace, 2016 55
  56. Primer 8 CompletableFuture.supplyAsync( () -> readPage("http://jodd.org/") ) .thenApply(Parser::getLinks(page)) .thenAcceptAsync(DisplayPanel::display(links)) );

    DAFEDLOVE | HeapSpace, 2016 56
  57. 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
  58. 4 @see JavaDoc DAFEDLOVE | HeapSpace, 2016 58

  59. C10K DAFEDLOVE | HeapSpace, 2016 59

  60. 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
  61. Pravilo Za Async je potrebno dvoje ❤ DAFEDLOVE | HeapSpace,

    2016 61
  62. 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
  63. 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
  64. Async Java AsynchronousChannel DAFEDLOVE | HeapSpace, 2016 64

  65. 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
  66. Hello VertX! http://vertx.io/ DAFEDLOVE | HeapSpace, 2016 66

  67. VertX 4 Polyglot 4 Jednostavan 4 Skalabilan 4 actor-like concurrency

    model DAFEDLOVE | HeapSpace, 2016 67
  68. 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
  69. 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
  70. NE BLOKIRAJ ULAZ (worker thread) DAFEDLOVE | HeapSpace, 2016 70

  71. Teh nEd (async ispis :) DAFEDLOVE | HeapSpace, 2016 71