Skip to content

Instantly share code, notes, and snippets.

@vengateshm
Created September 23, 2021 17:28
Show Gist options
  • Save vengateshm/c00676abc3d084f2b920a02def4117e4 to your computer and use it in GitHub Desktop.
Save vengateshm/c00676abc3d084f2b920a02def4117e4 to your computer and use it in GitHub Desktop.
This file has snippets of different coroutines concept
import kotlinx.coroutines.*
import kotlin.random.Random
import kotlin.system.measureTimeMillis
fun main(args: Array<String>) {
// measureCoroutineExecutionTime()
// catchExceptionOfLaunchCoroutineBuilder()
// catchExceptionOfAsyncCoroutineBuilder()
// fireAndForget()
// getReturnValueFromCoroutine()
// parallelExecutionUsingCoroutineScopeBuilder()
// coroutineWithTimeout()
// cancellingCoroutine()
}
fun cancellingCoroutine() {
runBlocking {
var i = 0
var j = 0
val scope = CoroutineScope(Dispatchers.Default)
val job1 = scope.launch {
while (true) {
delay(Random.nextLong(1, 10) * 10)
i += 1
}
}
val job2 = scope.launch {
while (true) {
delay(Random.nextLong(1, 10) * 10)
j += 1
}
}
while (true) {
print("Enter command (current, cancel or exit): ")
when (readLine()) {
"current" -> {
println("Current value of job1 is $i")
println("Current value of job2 is $j")
}
"cancel" -> {
job1.cancel()
println("Final value of job1 was $i")
println("Final value of job2 was $j")
}
"exit" -> {
println("Thank you, come again!")
break
}
else -> {
println("Command must be current, cancel or exit")
}
}
}
}
}
fun coroutineWithTimeout() {
runBlocking {
try {
withTimeout(5000) {
var i = 0
while (true) {
delay(1000)
i++
println(i)
}
}
} catch (e: Exception) {
println("Coroutine cancelled on time out!")
}
}
}
fun parallelExecutionUsingCoroutineScopeBuilder() {
val time = measureTimeMillis {
runBlocking {
coroutineScope {
// If one child coroutine fails then
// rest of the child coroutines will be cancelled
launch {
delay(0)
println("C1")
}
launch {
delay(1000)
println("C2")
throw Exception("Exception")
}
launch {
delay(2000)
println("C3")
}
}
}
}
println("${time / 1000}s")
}
fun getReturnValueFromCoroutine() {
runBlocking {
val deferred = async {
getStringValue()
}
println(deferred.await().uppercase())
}
}
suspend fun getStringValue(): String {
delay(4000)
return "I am String value!"
}
fun fireAndForget() {
runBlocking {
launch {
delay(4000)
println("Fire and forget!")
}
}
}
fun catchExceptionOfAsyncCoroutineBuilder() {
runBlocking {
val scope = CoroutineScope(Dispatchers.Default)
val deferred = scope.async {
try {
delayAndThrowException()
} catch (e: Exception) {
println(e.message)
}
}
deferred.join()
}
}
fun catchExceptionOfLaunchCoroutineBuilder() {
runBlocking {
val coroutineScope = CoroutineScope(Dispatchers.Default)
val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
println(throwable.message)
}
val job = coroutineScope.launch(exceptionHandler) {
delayAndThrowException()
}
job.join()
}
}
suspend fun delayAndThrowException() {
delay(4000)
throw Exception("Timed out!")
}
fun measureCoroutineExecutionTime() {
// Measure two coroutines execution time
val time = measureTimeMillis {
runBlocking {
launch {
delay(10000)
println("Coroutine 1")
}
launch {
delay(10000)
println("Coroutine 2")
}
}
}
println("${time / 1000}s")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment