Created
January 10, 2019 22:52
-
-
Save Bill/9729fc8b7de9dd56fafa8f9865fa8c2c to your computer and use it in GitHub Desktop.
Little dispatcher exerciser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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