Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I know what the number of threads is used by specific coroutines dispatcher?

How could I know what the number of threads Dispatchers.IO is currently using ?

like image 406
dkzm Avatar asked Apr 10 '19 08:04

dkzm


1 Answers

As you can read here Dispatchers.IO doesn't have own pool of threads, it uses a shared pool. Dispatchers.Default uses the same pool of threads. There no simple way to get active threads currently used by Dispatchers.IO. But you can try get thread count inside shared pool of threads. The common pool of threads is creating inside CommonPool.kt. It can create own pool or use ForkJoinPool. All threads created in pool have a specific name. So you can find all active thread of shared pool by name.

val threads = Thread.getAllStackTraces().keys.filter {
    it.name.startsWith("CommonPool") || it.name.startsWith("ForkJoinPool")
}
threads.size
like image 185
Andrew Churilo Avatar answered Sep 30 '22 17:09

Andrew Churilo