Skip to content

Instantly share code, notes, and snippets.

@biskandar
Last active May 21, 2019 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biskandar/a39467c92f84320b6af9183efca34750 to your computer and use it in GitHub Desktop.
Save biskandar/a39467c92f84320b6af9183efca34750 to your computer and use it in GitHub Desktop.
Test Kotlin Coroutines
import kotlinx.coroutines.*
import java.time.Instant
import java.time.format.DateTimeFormatter
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
fun main(args: Array<String>) {
exampleBlocking()
exampleBlockingDispatcher()
exampleLaunchGlobal()
exampleLaunchLocal()
exampleLaunchLocalDispatcher()
exampleLaunchLocalThreadPool()
exampleAsynchAwait()
exampleAsynchAwaitContext()
}
fun display(message: String) {
println(
DateTimeFormatter.ISO_INSTANT.format(Instant.now())
.plus(" ")
.plus("${Thread.currentThread().name}")
.plus(" ")
.plus(message)
)
}
suspend fun displayDelay(message: String) {
// Thread.sleep(1000)
delay(1000) // kotlin way : delay , suspend , runBlocking
display(message)
}
suspend fun calculateHardThings(startNum: Int): Int {
delay(1000)
return startNum * 10
}
fun exampleBlocking() {
println("--- exampleBlocking()")
display("one")
runBlocking {
displayDelay("two")
}
display("three");
}
fun exampleBlockingDispatcher() {
println("--- exampleBlockingDispatcher()")
display("one")
runBlocking(Dispatchers.Default) {
displayDelay("two")
}
display("three")
}
fun exampleLaunchGlobal() = runBlocking {
println("--- exampleLaunchGlobal()")
display("one")
val job = GlobalScope.launch {
displayDelay("two")
}
display("three")
job.join()
}
fun exampleLaunchLocal() = runBlocking {
println("--- exampleLaunchLocal()")
display("one")
launch {
displayDelay("two")
}
display("three")
}
fun exampleLaunchLocalDispatcher() = runBlocking {
println("--- exampleLaunchLocalDispatcher()")
display("one")
launch(Dispatchers.Default) {
displayDelay("two")
}
display("three")
}
fun exampleLaunchLocalThreadPool() = runBlocking {
println("--- exampleLaunchLocalThreadPool()")
display("one")
val myDispatcher = Executors.newFixedThreadPool(2).asCoroutineDispatcher()
launch(myDispatcher) {
displayDelay("two")
}
display("three")
(myDispatcher.executor as ExecutorService).shutdown()
}
fun exampleAsynchAwait() = runBlocking {
val startTime = System.currentTimeMillis()
val deferred1 = async { calculateHardThings(10) } // run concurrently
val deferred2 = async { calculateHardThings(20) } // run concurrently
val deferred3 = async { calculateHardThings(30) } // run concurrently
val sum = deferred1.await() + deferred2.await() + deferred3.await()
val endTime = System.currentTimeMillis()
println("async wait result = ${sum}, took = ${(endTime - startTime) / 1000}s")
}
fun exampleAsynchAwaitContext() = runBlocking {
val startTime = System.currentTimeMillis()
val deferred1 = withContext(Dispatchers.Default) { calculateHardThings(11) }
val deferred2 = withContext(Dispatchers.Default) { calculateHardThings(20) }
val deferred3 = withContext(Dispatchers.Default) { calculateHardThings(30) }
val sum = deferred1 + deferred2 + deferred3
val endTime = System.currentTimeMillis()
println("async wait context result = ${sum}, took = ${(endTime - startTime) / 1000}s")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment