Skip to content

Instantly share code, notes, and snippets.

@GauravChaddha1996
Created January 30, 2021 18:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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