Skip to content

Instantly share code, notes, and snippets.

@GauravChaddha1996
Created January 30, 2021 18:14
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 GauravChaddha1996/f6ab92a1540373e261beafcc4d02cb42 to your computer and use it in GitHub Desktop.
Save GauravChaddha1996/f6ab92a1540373e261beafcc4d02cb42 to your computer and use it in GitHub Desktop.
suspend fun t4(): String {
println("Inside t4() on ${Thread.currentThread().name}")
try {
// Emulate reading a file
delay(100)
return "resultT4"
} catch (e: Exception) {
// Ignore exception handling for now
return ""
}
}
// A way to make custom dispatcher using Executors
object CustomDispatchers {
val DB = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
}
// Scope is passed so we can launch our db operation while respecting the scope
suspend fun t5(scope: CoroutineScope, param4: String): String {
// Since this is a db entry operation we run this block
// using the DB dispatcher we made.
return scope.async(CustomDispatchers.DB) {
println("Inside t5() on ${Thread.currentThread().name}")
try {
// Emulate db entry operation
delay(100)
return@async param4.plus("_").plus("resultT5")
} catch (e: Exception) {
// Ignore exception handling for now
return@async ""
}
}.await()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment