Both Java and Kotlin are languages that are heavily used in Distributed Systems, handling multiple asynchronous operations simultaneously. Blocking is not an option in such an environment, but let’s be honest, we crave for the easier synchronous world. That’s why JVM as a platform has an inherent focus on providing an 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.
48. DIRECT STYLE CODE
val location = getLocationsNearKnownPostcode()
val category = location.classification.category
ActionResult
Continuation
ACTION + RESULT & CONTINUATION
71. USER/KERNEL SPACE BORDER
CPU SCHEDULER JVM
FORKJOINSCHEDULER
THREAD
Instruction 1
Instruction 2
Instruction 3
Instruction 4
Coroutines
72. CONTINUATION INTERFACE
public class Continuation implements Runnable {
public Continuation(ContinuationScope s, Runnable r)
public final void run()
public static void yield(ContinuationScope s)
public boolean isDone()
}
73. CONTINUATIONS
public static void main(String[] args){
ContinuationScope scope = new ContinuationScope("scope")
Continuation continuation = new Continuation(scope, () ->{
var location = getLocationsNearKnownPostcode()
var category = location.classification.category
if (category.equals(”Store”)) {
var openingHours = getOpeningHours(location.id)
println("Store Opening Hours: $openingHours")
}
if (category.equals(“Office")) {
payInvoice(location.id)
println("Invoice paid")
}
})
while (!continuation.isDone()) {
continuation.run();
}
}
public List<LocationWrapper> getLocationNearKnownPostcode() {
return getLocationsNearCoordinates("SW8 5YY, UK”).first()
}
public OpeningHours getOpeningHours(id: String) {
return getStoreDetails(id).openingHours
}
public void payInvoice(id: String) {
return payOfficeRent(id)
}
74. CONTINUATIONS
public static void main(String[] args){
ContinuationScope scope = new ContinuationScope("scope")
Continuation continuation = new Continuation(scope, () ->{
var location = getLocationsNearKnownPostcode()
var category = location.classification.category
if (category.equals(”Store”)) {
var openingHours = getOpeningHours(location.id)
println("Store Opening Hours: $openingHours")
}
if (category.equals(“Office")) {
payInvoice(location.id)
println("Invoice paid")
}
})
while (!continuation.isDone()) {
continuation.run();
}
}
public List<LocationWrapper> getLocationNearKnownPostcode() {
return getLocationsNearCoordinates("SW8 5YY, UK”).first()
}
public OpeningHours getOpeningHours(id: String) {
return getStoreDetails(id).openingHours
}
public void payInvoice(id: String) {
return payOfficeRent(id)
}
75. CONTINUATIONS
public static void main(String[] args){
ContinuationScope scope = new ContinuationScope("scope")
Continuation continuation = new Continuation(scope, () ->{
var location = getLocationsNearKnownPostcode()
var category = location.classification.category
if (category.equals(”Store”)) {
var openingHours = getOpeningHours(location.id)
println("Store Opening Hours: $openingHours")
}
if (category.equals(“Office")) {
payInvoice(location.id)
println("Invoice paid")
}
})
while (!continuation.isDone()) {
continuation.run();
}
}
public List<LocationWrapper> getLocationNearKnownPostcode() {
return getLocationsNearCoordinates("SW8 5YY, UK”).first()
}
public OpeningHours getOpeningHours(id: String) {
return getStoreDetails(id).openingHours
}
public void payInvoice(id: String) {
return payOfficeRent(id)
}
76. CONTINUATIONS
public static void main(String[] args){
ContinuationScope scope = new ContinuationScope("scope")
Continuation continuation = new Continuation(scope, () ->{
var location = getLocationsNearKnownPostcode()
var category = location.classification.category
if (category.equals(”Store”)) {
var openingHours = getOpeningHours(location.id)
println("Store Opening Hours: $openingHours")
}
if (category.equals(“Office")) {
payInvoice(location.id)
println("Invoice paid")
}
})
while (!continuation.isDone()) {
continuation.run();
}
}
public List<LocationWrapper> getLocationNearKnownPostcode() {
return getLocationsNearCoordinates("SW8 5YY, UK”).first()
}
public OpeningHours getOpeningHours(id: String) {
return getStoreDetails(id).openingHours
}
public void payInvoice(id: String) {
return payOfficeRent(id)
}
77. CONTINUATIONS
public static void main(String[] args){
ContinuationScope scope = new ContinuationScope("scope")
Continuation continuation = new Continuation(scope, () ->{
var location = getLocationsNearKnownPostcode()
var category = location.classification.category
if (category.equals(”Store”)) {
var openingHours = getOpeningHours(location.id)
println("Store Opening Hours: $openingHours")
}
if (category.equals(“Office")) {
payInvoice(location.id)
println("Invoice paid")
}
})
while (!continuation.isDone()) {
continuation.run();
}
}
public List<LocationWrapper> getLocationNearKnownPostcode() {
return getLocationsNearCoordinates("SW8 5YY, UK”).first()
}
public OpeningHours getOpeningHours(id: String) {
return getStoreDetails(id).openingHours
}
public void payInvoice(id: String) {
return payOfficeRent(id)
}
78. CONTINUATIONS
public static void main(String[] args){
ContinuationScope scope = new ContinuationScope("scope")
Continuation continuation = new Continuation(scope, () ->{
var location = getLocationsNearKnownPostcode()
var category = location.classification.category
if (category.equals(”Store”)) {
var openingHours = getOpeningHours(location.id)
println("Store Opening Hours: $openingHours")
}
if (category.equals(“Office")) {
payInvoice(location.id)
println("Invoice paid")
}
})
while (!continuation.isDone()) {
continuation.run();
}
}
public List<LocationWrapper> getLocationNearKnownPostcode() {
return getLocationsNearCoordinates("SW8 5YY, UK”).first()
}
public OpeningHours getOpeningHours(id: String) {
return getStoreDetails(id).openingHours
}
public void payInvoice(id: String) {
return payOfficeRent(id)
}
94. S O U R C E S
http://cr.openjdk.java.net/~rpressler/loom/Loom-Proposal.html
https://www.youtube.com/watch?v=YrrUCSi72E8
KOTLINCONF 2017 - DEEP DIVE INTO COROUTINES ON JVM BY ROMAN ELIZAROV
https://www.youtube.com/watch?v=vbGbXUjlRyQ
PROJECT LOOM: FIBERS AND CONTINUATIONS FOR JAVA BY ALAN BATEMAN
PROJECT LOOM: FIBERS AND CONTINUATIONS FOR THE JAVA VIRTUAL MACHINE