Slide 1

Slide 1 text

https://www.maxpixel.net/Post-Impressionist-Post-Impressionism-Fine-Art-Dutch-1428139

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

? computer (n.)

Slide 5

Slide 5 text

1640s, "one who calculates, a reckoner, one whose occupation is to make arithmetical calculations," computer (n.)

Slide 6

Slide 6 text

https://commons.wikimedia.org/wiki/File:Women_holding_parts_of_the_first_four_Army_computers.jpg

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

https://www.computerhistory.org/revolution/punched-cards/2/211/2253

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

https://youtu.be/Q07PhW5sCEk

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

private Connection.Available getConnection(long eta, long wait, String token) { for(;;) { if (eta > MAX_ETA_MS) { throw new EtaExceededException(); } if (wait > 0) { Thread.sleep(wait); } println("Retrying download after " + wait + "ms wait."); Connection c = coordinator.requestConnection(token); if (c instanceof Connection.Available) { return (Connection.Available) c; } Connection.Unavailable unavail = (Connection.Unavailable) c; eta = unavail.getEta(); wait = unavail.getWait(); token = unavail.getToken(); } }

Slide 17

Slide 17 text

private Connection.Available getConnection(long eta, long wait, String token) { for(;;) { if (eta > MAX_ETA_MS) { throw new EtaExceededException(); } if (wait > 0) { Thread.sleep(wait); } println("Retrying download after " + wait + "ms wait."); Connection c = coordinator.requestConnection(token); if (c instanceof Connection.Available) { return (Connection.Available) c; } Connection.Unavailable unavail = (Connection.Unavailable) c; eta = unavail.getEta(); wait = unavail.getWait(); token = unavail.getToken(); } }

Slide 18

Slide 18 text

private Connection.Available getConnection(long eta, long wait, String token) { for(;;) { if (eta > MAX_ETA_MS) { throw new EtaExceededException(); } if (wait > 0) { Thread.sleep(wait); } println("Retrying download after " + wait + "ms wait."); Connection c = coordinator.requestConnection(token); if (c instanceof Connection.Available) { return (Connection.Available) c; } Connection.Unavailable unavail = (Connection.Unavailable) c; eta = unavail.getEta(); wait = unavail.getWait(); token = unavail.getToken(); } }

Slide 19

Slide 19 text

private Connection.Available getConnection(long eta, long wait, String token) { for(;;) { if (eta > MAX_ETA_MS) { throw new EtaExceededException(); } if (wait > 0) { Thread.sleep(wait); } println("Retrying download after " + wait + "ms wait."); Connection c = coordinator.requestConnection(token); if (c instanceof Connection.Available) { return (Connection.Available) c; } Connection.Unavailable unavail = (Connection.Unavailable) c; eta = unavail.getEta(); wait = unavail.getWait(); token = unavail.getToken(); } }

Slide 20

Slide 20 text

private Connection.Available getConnection(long eta, long wait, String token) { for(;;) { if (eta > MAX_ETA_MS) { throw new EtaExceededException(); } if (wait > 0) { Thread.sleep(wait); } println("Retrying download after " + wait + "ms wait."); Connection c = coordinator.requestConnection(token); if (c instanceof Connection.Available) { return (Connection.Available) c; } Connection.Unavailable unavail = (Connection.Unavailable) c; eta = unavail.getEta(); wait = unavail.getWait(); token = unavail.getToken(); } }

Slide 21

Slide 21 text

private Connection.Available getConnection(long eta, long wait, String token) { for(;;) { if (eta > MAX_ETA_MS) { throw new EtaExceededException(); } if (wait > 0) { Thread.sleep(wait); } println("Retrying download after " + wait + "ms wait."); Connection c = coordinator.requestConnection(token); if (c instanceof Connection.Available) { return (Connection.Available) c; } Connection.Unavailable unavail = (Connection.Unavailable) c; eta = unavail.getEta(); wait = unavail.getWait(); token = unavail.getToken(); } }

Slide 22

Slide 22 text

private Connection.Available getConnection(long eta, long wait, String token) { for(;;) { if (eta > MAX_ETA_MS) { throw new EtaExceededException(); } if (wait > 0) { Thread.sleep(wait); } println("Retrying download after " + wait + "ms wait."); Connection c = coordinator.requestConnection(token); if (c instanceof Connection.Available) { return (Connection.Available) c; } Connection.Unavailable unavail = (Connection.Unavailable) c; eta = unavail.getEta(); wait = unavail.getWait(); token = unavail.getToken(); } }

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

public static InputStream blockingRequest(String url, String headers) throws IOException { URL uri = new URL(url); SocketAddress serverAddress = new InetSocketAddress(uri.getHost(), uri.getPort() SocketChannel channel = SocketChannel.open(serverAddress); ByteBuffer buffer = ByteBuffer.wrap((headers + "Host: " + uri.getHost() + "\r\n\ do { channel.write(buffer); } while(buffer.hasRemaining()); return channel.socket().getInputStream(); }

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

https://blog.paralleluniverse.co/2014/02/04/littles-law/

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

private void getConnection(long eta, long wait, String token, CompletionHandler handler) { if (eta > MAX_ETA_MS) { if (handler!=null) handler.failed(new EtaExceededException()); } boundedServiceExecutor.schedule(() -> { println("Retrying download after " + wait + "ms wait."); coordinator.requestConnection(token, new CompletionHandler<>() { @Override public void completed(Connection c) { if (c instanceof Connection.Available) { if (handler!=null) handler.completed((Connection.Available) c); } else { Connection.Unavailable unavail = (Connection.Unavailable) c; getConnection( unavail.getEta(), unavail.getWait(), unavail.getToken(), handler); } } @Override public void failed(Throwable t) { ...

Slide 38

Slide 38 text

private void getConnection(long eta, long wait, String token, CompletionHandler handler) { if (eta > MAX_ETA_MS) { if (handler!=null) handler.failed(new EtaExceededException()); } boundedServiceExecutor.schedule(() -> { println("Retrying download after " + wait + "ms wait."); coordinator.requestConnection(token, new CompletionHandler<>() { @Override public void completed(Connection c) { if (c instanceof Connection.Available) { if (handler!=null) handler.completed((Connection.Available) c); } else { Connection.Unavailable unavail = (Connection.Unavailable) c; getConnection( unavail.getEta(), unavail.getWait(), unavail.getToken(), handler); } } @Override public void failed(Throwable t) { ...

Slide 39

Slide 39 text

private void getConnection(long eta, long wait, String token, CompletionHandler handler) { if (eta > MAX_ETA_MS) { if (handler!=null) handler.failed(new EtaExceededException()); } boundedServiceExecutor.schedule(() -> { println("Retrying download after " + wait + "ms wait."); coordinator.requestConnection(token, new CompletionHandler<>() { @Override public void completed(Connection c) { if (c instanceof Connection.Available) { if (handler!=null) handler.completed((Connection.Available) c); } else { Connection.Unavailable unavail = (Connection.Unavailable) c; getConnection( unavail.getEta(), unavail.getWait(), unavail.getToken(), handler); } } @Override public void failed(Throwable t) { ...

Slide 40

Slide 40 text

private void getConnection(long eta, long wait, String token, CompletionHandler handler) { if (eta > MAX_ETA_MS) { if (handler!=null) handler.failed(new EtaExceededException()); } boundedServiceExecutor.schedule(() -> { println("Retrying download after " + wait + "ms wait."); coordinator.requestConnection(token, new CompletionHandler<>() { @Override public void completed(Connection c) { if (c instanceof Connection.Available) { if (handler!=null) handler.completed((Connection.Available) c); } else { Connection.Unavailable unavail = (Connection.Unavailable) c; getConnection( unavail.getEta(), unavail.getWait(), unavail.getToken(), handler); } } @Override public void failed(Throwable t) { ...

Slide 41

Slide 41 text

private void getConnection(long eta, long wait, String token, CompletionHandler handler) { if (eta > MAX_ETA_MS) { if (handler!=null) handler.failed(new EtaExceededException()); } boundedServiceExecutor.schedule(() -> { println("Retrying download after " + wait + "ms wait."); coordinator.requestConnection(token, new CompletionHandler<>() { @Override public void completed(Connection c) { if (c instanceof Connection.Available) { if (handler!=null) handler.completed((Connection.Available) c); } else { Connection.Unavailable unavail = (Connection.Unavailable) c; getConnection( unavail.getEta(), unavail.getWait(), unavail.getToken(), handler); } } @Override public void failed(Throwable t) { ...

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

public static InputStream blockingRequest(String url, String headers) throws IOException { URL uri = new URL(url); SocketAddress serverAddress = new InetSocketAddress(uri.getHost(), uri.getPort() SocketChannel channel = SocketChannel.open(serverAddress); ByteBuffer buffer = ByteBuffer.wrap((headers + "Host: " + uri.getHost() + "\r\n\ do { channel.write(buffer); } while(buffer.hasRemaining()); return channel.socket().getInputStream(); }

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

public static void asyncNonBlockingRequest(ExecutorService executor, String url, String headers, RequestHandler handler) { executor.submit(() -> { try { println("Starting request to " + url); URL uri = new URL(url); SocketAddress serverAddress = new InetSocketAddress(uri.getHost(), 80); AsynchronousSocketChannel channel = AsynchronousSocketChannel.open(group); channel.connect(serverAddress, null, new CompletionHandler() { @Override public void completed(Void result, Void attachment) { ByteBuffer headersBuffer = ByteBuffer.wrap((headers + "Host: " + uri.getHost())); ByteBuffer responseBuffer = ByteBuffer.allocate(1024); channel.write(headersBuffer, headersBuffer, new CompletionHandler<>() { @Override public void completed(Integer written, ByteBuffer attachment) { if (attachment.hasRemaining()) { channel.write(attachment, attachment, this); } else { channel.read(responseBuffer, responseBuffer, new CompletionHandler<>() { @Override public void completed(Integer read, ByteBuffer attachment) { // More // this // way // ==>

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

https://medium.com/@copyconstruct/the-method-to-epolls-madness-d9d2d6378642

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Flux.interval(Duration.ofSeconds(2L)) .flatMap(l -> coordinator.heartbeat(conn.getToken())) .doOnNext(c -> println("Pulse!")) .then() .doOnTerminate(() -> println("Pulse terminated"));

Slide 57

Slide 57 text

private Mono getConnection(long eta, long wait, String token) { AtomicLong etaRef = new AtomicLong(eta); AtomicLong waitRef = new AtomicLong(wait); AtomicReference tokenRef = new AtomicReference<>(token); return Mono.defer(() -> { if (etaRef.get() > MAX_ETA_MS) { return Mono.error(new EtaExceededException()); } return Mono.delay(Duration.ofMillis(waitRef.get())) .flatMap(i -> coordinator.requestConnection(tokenRef.get())); }).flatMap(c -> { if (c instanceof Connection.Available) { return Mono.just((Connection.Available) c); } else { Connection.Unavailable unavail = (Connection.Unavailable) c; etaRef.set(unavail.getEta()); waitRef.set(unavail.getWait()); tokenRef.set(unavail.getToken()); return Mono.empty(); } }).repeatWhenEmpty(Repeat .onlyIf(ctx -> true) .doOnRepeat(ctx -> println(waitRef.get() + ", " + etaRef.get() + ", " + tokenRef.get()))); }

Slide 58

Slide 58 text

private Mono getConnection(long eta, long wait, String token) { AtomicLong etaRef = new AtomicLong(eta); AtomicLong waitRef = new AtomicLong(wait); AtomicReference tokenRef = new AtomicReference<>(token); return Mono.defer(() -> { if (etaRef.get() > MAX_ETA_MS) { return Mono.error(new EtaExceededException()); } return Mono.delay(Duration.ofMillis(waitRef.get())) .flatMap(i -> coordinator.requestConnection(tokenRef.get())); }).flatMap(c -> { if (c instanceof Connection.Available) { return Mono.just((Connection.Available) c); } else { Connection.Unavailable unavail = (Connection.Unavailable) c; etaRef.set(unavail.getEta()); waitRef.set(unavail.getWait()); tokenRef.set(unavail.getToken()); return Mono.empty(); } }).repeatWhenEmpty(Repeat .onlyIf(ctx -> true) .doOnRepeat(ctx -> println(waitRef.get() + ", " + etaRef.get() + ", " + tokenRef.get()))); }

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

class StateMachineIterator implements Iterator { private int state; private int i; public String next() { switch(state) { case 0: state=1; return "A"; case 1: state=2; i=0; return "B"; case 2: if(i == 3) state = 3; return "C" + (i++); case 3: state=4; return "D"; case 4: state=5; return "E"; default: throw new NoSuchElementException(); } } public boolean hasNext() { return state < 5; } public void remove() { throw new UnsupportedOperationException("Not supported"); } }

Slide 68

Slide 68 text

class StateMachineIterator implements Iterator { private int state; private int i; public String next() { switch(state) { case 0: state=1; return "A"; case 1: state=2; i=0; return "B"; case 2: if(i == 3) state = 3; return "C" + (i++); case 3: state=4; return "D"; case 4: state=5; return "E"; default: throw new NoSuchElementException(); } } public boolean hasNext() { return state < 5; } public void remove() { throw new UnsupportedOperationException("Not supported"); } }

Slide 69

Slide 69 text

class StateMachineIterator implements Iterator { private int state; private int i; public String next() { switch(state) { case 0: state=1; return "A"; case 1: state=2; i=0; return "B"; case 2: if(i == 3) state = 3; return "C" + (i++); case 3: state=4; return "D"; case 4: state=5; return "E"; default: throw new NoSuchElementException(); } } public boolean hasNext() { return state < 5; } public void remove() { throw new UnsupportedOperationException("Not supported"); } }

Slide 70

Slide 70 text

class StateMachineIterator implements Iterator { private int state; private int i; public String next() { switch(state) { case 0: state=1; return "A"; case 1: state=2; i=0; return "B"; case 2: if(i == 3) state = 3; return "C" + (i++); case 3: state=4; return "D"; case 4: state=5; return "E"; default: throw new NoSuchElementException(); } } public boolean hasNext() { return state < 5; } public void remove() { throw new UnsupportedOperationException("Not supported"); } }

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

private Connection.Available getConnection(long eta, long wait, String token) { for(;;) { if (eta > MAX_ETA_MS) { throw new EtaExceededException(); } if (wait > 0) { Thread.sleep(wait); } println("Retrying download after " + wait + "ms wait."); Connection c = coordinator.requestConnection(token); if (c instanceof Connection.Available) { return (Connection.Available) c; } Connection.Unavailable unavail = (Connection.Unavailable) c; eta = unavail.getEta(); wait = unavail.getWait(); token = unavail.getToken(); } }

Slide 75

Slide 75 text

Connection.Available conn = getConnection(); Runnable pulse = makePulse(conn); Fiber f = null; try (InputStream content = gateway.downloadThingy()) { f = FiberScope.background().schedule(pulse); ignoreContent(content); } catch (IOException e) { err("Download failed."); throw e; } finally { if (f!=null) { f.cancel(); } }

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

http://250bpm.com/blog:71

Slide 86

Slide 86 text

http://250bpm.com/blog:71

Slide 87

Slide 87 text

Connection.Available conn = getConnection(); Runnable pulse = makePulse(conn); Fiber f = null; try (InputStream content = gateway.downloadThingy()) { f = FiberScope.background().schedule(pulse); ignoreContent(content); } catch (IOException e) { err("Download failed."); throw e; } finally { if (f!=null) { f.cancel(); } }

Slide 88

Slide 88 text

try (FiberScope scope = FiberScope.open(CANCEL_AT_CLOSE, PROPAGATE_CANCEL)) { var conn = getConnection(); try (InputStream content = gateway.downloadThingy()) { Runnable pulse = makePulse(conn, i); scope.schedule(pulse); ignoreContent(content); } } catch (IOException e) { err("Download failed."); throw e; }

Slide 89

Slide 89 text

Papers and Webpages Flynn, Michael J. - Some Computer Organizations and Their Effectiveness Haynes, Christopher T. - Logic Continuations Wand, Mitchell - Continuation-based Multiprocessing Loitsch, Florian - Exceptional Continuations in JavaScript Long, James - What's In A Continuation Reynolds, John C. - The Discoveries of Continuations Clinger, Hartheimer, Ost - Implementation Strategies for Continuations Haynes, Friedman, Wand - Obtaining Coroutines with Continuations Pressler, Ron - Why Continuations are Coming to Java Repos https://github.com/forax/loom-fiber/ https://bitbucket.org/arnaudbos/untangled/

Slide 90

Slide 90 text

No content