uli
02/23/2017, 9:09 AMval massive = 1_000_000
private suspend fun aMassiveRun(context: CoroutineContext = CommonPool, action: suspend () -> Unit) {
val jobs = List(massive) {
launch(context) {
action()
}
}
jobs.forEach { it.join() }
}
private object ThreadConfinement1: Callable<Int> {
val counterContext = newSingleThreadContext("CounterContext")
override fun call(): Int {
var counter = 0
runBlocking<Unit> {
aMassiveRun(counterContext) {
counter++
}
}
return counter
}
}
private object ThreadConfinement2: Callable<Int> {
val counterContext = newSingleThreadContext("CounterContext")
override fun call(): Int {
var counter = 0
runBlocking<Unit> {
aMassiveRun {
run(counterContext) {
counter++
}
}
}
return counter
}
}
I time both callables 5 times to warm up hotspot and only take the last measurement.
Both tests run all incrementation on a single thread but ThreadConfinement2 does the scheduling of the actual work in form the CommonPool.
elizarov
02/23/2017, 9:47 AMuli
02/23/2017, 12:50 PM