Gopal S Akshintala
08/24/2020, 4:03 AMIOPool
in conjunction with parTraverse
in the right way? I couldn't find any documentation about it.
@Test
fun `IODispatcher + parTraverse + blocking`() {
val time = measureTimeMillis {
runBlocking {
evalOn(IODispatchers.IOPool) {
(1..20).parTraverse {
blockingWork(it)
}
}
}
}
println("Done in $time ms [${Thread.currentThread().name}]")
}
private fun blockingWork(i: Int) {
Thread.sleep(1000)
println("Work $i done [${Thread.currentThread().name}]")
}
stojan
08/24/2020, 10:25 AMparTraverse(IODispatchers.IOPool) { // effect here }
methodstojan
08/24/2020, 10:25 AMevalOn
Gopal S Akshintala
08/24/2020, 10:46 AMGopal S Akshintala
08/24/2020, 10:50 AM@Test
fun `parTraverse + blocking`() {
val time = measureTimeMillis {
runBlocking {
(1..20).parTraverse {
blockingWork(it)
}
}
}
// 3s, multiple blocked bg threads
println("Done in $time ms [${Thread.currentThread().name}]")
}
@Test
fun `IOPool + parTraverse + blocking`() {
val time = measureTimeMillis {
runBlocking {
(1..20).parTraverse(IODispatchers.IOPool) {
blockingWork(it)
}
}
}
// 1s
println("Done in $time ms [${Thread.currentThread().name}]")
}
stojan
08/24/2020, 10:53 AMIOPool
is backed by unbounded thread pool, so it's suitable for blocking work
not sure what the default thread pool is (I would assume computation, which is backed by a thread pool with a cap determined by number of cores/processors)Gopal S Akshintala
08/24/2020, 10:59 AMForkJoinPool-1-worker-15
on my 4 core machine for default dispatcher, but just realized there can be some hyperthreading on my corestojan
08/24/2020, 3:22 PMGopal S Akshintala
08/24/2020, 3:23 PMGopal S Akshintala
08/24/2020, 3:23 PMstojan
08/24/2020, 3:24 PM