Reputation: 1299
How could I know what the number of threads Dispatchers.IO is currently using ?
Upvotes: 7
Views: 2995
Reputation: 2309
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
Upvotes: 2
Reputation: 17
You can use the Android profiler to monitor threads.
Android Monitor tools were replaced with Android Profiler, in Android Studio 3.0:
Android Profiler - Android Studio 3.0 includes a brand new suite of tools to help debug performance problems in your app. We completely rewrote the previous set of Android Monitor tools, and replaced them with the Android Profiler. Once you deploy your app to a running device or emulator, click on the Android Profiler tab and you will now have access to a real-time & unified view of the CPU, Memory, & Network activity for your app. ...
To monitor threads, use CPU Profiler in Android Profiler.
- Click View > Tool Windows > Android Profiler (you can also click Android Profiler in the toolbar).
- Select the device and app process you want to profile from the Android Profiler toolbar. If you've connected a device over USB but don't see it listed, ensure that you have enabled USB debugging.
- Click anywhere in the CPU timeline to open the CPU Profiler.
From: https://stackoverflow.com/a/50691178/2675353
Upvotes: 0