Skip to content

Instantly share code, notes, and snippets.

@Bill Bill/Foo.kt
Created Jan 10, 2019

Embed
What would you like to do?
Little dispatcher exerciser
import kotlinx.coroutines.*
import kotlinx.coroutines.launch;
import kotlinx.coroutines.scheduling.ExperimentalCoroutineDispatcher
import kotlinx.coroutines.test.setMain
import java.util.concurrent.TimeUnit
import kotlin.coroutines.coroutineContext
suspend fun log(msg: String) = println("[${Thread.currentThread().name} ${coroutineContext[CoroutineName]}] $msg")
fun main(args: Array<String>) = repeat(1) {
println("--------------------------------------------------------")
val simulated = SimulationDispatcher().blockingSimulated(1)
runBlocking<Unit>(simulated) {
log("Start")
generateAmbientLoad()
launch {
delay(2000)
log("Stop")
}
}
}
private fun CoroutineScope.generateAmbientLoad() {
repeat(5) {
// launch(Dispatchers.Main + CoroutineName("Foo")) {
launch(CoroutineName("Foo")) {
delay(2000)
log("Hello $it")
}
}
}
@UseExperimental(InternalCoroutinesApi::class)
class SimulationDispatcher :
ExperimentalCoroutineDispatcher(1, 1, TimeUnit.SECONDS.toNanos(60L), "SimulationDispatcher")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.