Skip to content

Instantly share code, notes, and snippets.

@Bill
Created January 10, 2019 22:52
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 Bill/9729fc8b7de9dd56fafa8f9865fa8c2c to your computer and use it in GitHub Desktop.
Save Bill/9729fc8b7de9dd56fafa8f9865fa8c2c to your computer and use it in GitHub Desktop.
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