Both Kotlin and Java are languages that are heavily used in Distributed Systems, handling multiple asynchronous operations simultanouesely. Blocking is not an option in such an environment, but let’s be honest, we crave for easier synchronous world. That’s why JVM as a platform have inherent focus on providing easier option of multithreaded programming. Today, I’d like to compare two of such options - Kotlin Coroutines and Project Loom, that want to provide fibers and continuations to core Java.
16. BRANCHING IS HARD
fun combineAsync(name1: String, name2: String): CompletableFuture<Image>{
val future1 = CompletableFuture.supplyAsync(name1-> ...)
val future2 = CompletableFuture.supplyAsync(name2-> ...)
return future1.thenCombine(future2){i1,i2 -> ...}
}
35. DIRECT STYLE CODE
Action + Result and Continuation
val location = getLocationsNearKnownPostcode()
val category = location.classification.category
ActionResult
Continuation
54. THREAD
USER/KERNEL SPACE BORDER
CPU SCHEDULER JVM
FORKJOINSCHEDULER
THREAD
Instruction 1
Instruction 2
Instruction 3
Instruction 4
Coroutines
55. public class Continuation implements Runnable {
public Continuation(ContinuationScope s, Runnable r)
public final void run()
public static void yield(ContinuationScope s)
public boolean isDone()
}