Show less code. More informative comments (still hides blocking calls

This commit is contained in:
Ignasi Marimon-Clos 2020-07-07 15:27:25 +02:00
parent 024710a6e0
commit 2b71abe78e
2 changed files with 20 additions and 18 deletions

View file

@ -56,24 +56,25 @@ public class Monitor {
// if we peek on the stream too early it probably won't have processed any element. // if we peek on the stream too early it probably won't have processed any element.
printMonitorState(monitor.state()); printMonitorState(monitor.state());
// #monitor
// exclude from rendered snippet // At this point, the application will continue to run and future
Thread.sleep(500); // invocations to `printMonitorState(flowMonitor)` will continue to show
// the progress in the stream
// #monitor // #monitor
// ... // Don't use `Thread#sleep` in your code. It's a blocking call
// that can starve the thread-pool.
Thread.sleep(500);
// sometime later, our code has progressed. We can peek in the stream // sometime later, our code has progressed. We can peek in the stream
// again to see what's the latest element processed // again to see what's the latest element processed
printMonitorState(monitor.state()); printMonitorState(monitor.state());
// #monitor // Don't use `CompletableFuture#get` in your code. It's a blocking call
// exclude from rendered snippet // that can starve the thread-pool.
run.second().toCompletableFuture().get(1, TimeUnit.SECONDS); run.second().toCompletableFuture().get(1, TimeUnit.SECONDS);
// #monitor
// #monitor
// Eventually, the stream completes and if we check the state it reports the streasm finished. // Eventually, the stream completes and if we check the state it reports the streasm finished.
printMonitorState(monitor.state()); printMonitorState(monitor.state());
// #monitor
run.second().toCompletableFuture().whenComplete((x, t) -> actorSystem.terminate()); run.second().toCompletableFuture().whenComplete((x, t) -> actorSystem.terminate());
} }

View file

@ -50,24 +50,25 @@ class Monitor {
// if we peek on the stream early enough it probably won't have processed any element. // if we peek on the stream early enough it probably won't have processed any element.
printMonitorState(flowMonitor) printMonitorState(flowMonitor)
// #monitor
// exclude from rendered snippet // At this point, the application will continue to run and future
Thread.sleep(500) // invocations to `printMonitorState(flowMonitor)` will continue to show
// the progress in the stream
// #monitor // #monitor
// ... // Don't use `Thread#sleep` in your code. It's a blocking call
// that can starve the thread-pool.
Thread.sleep(500)
// sometime later, our code has progressed. We can peek in the stream // sometime later, our code has progressed. We can peek in the stream
// again to see what's the latest element processed // again to see what's the latest element processed
printMonitorState(flowMonitor) printMonitorState(flowMonitor)
// #monitor // Don't use `Await#result` in your code. It's a blocking call
// exclude from rendered snippet // that can starve the thread-pool.
Await.result(monitoredStream._2, 3.seconds) Await.result(monitoredStream._2, 3.seconds)
// #monitor
// #monitor
// Eventually, the stream completes and if we check the state it reports the streasm finished. // Eventually, the stream completes and if we check the state it reports the streasm finished.
printMonitorState(flowMonitor) printMonitorState(flowMonitor)
// #monitor
monitoredStream._2.onComplete(_ => system.terminate()) monitoredStream._2.onComplete(_ => system.terminate())