我怎么知道特定协程调度程序使用的线程数是多少?
How could I know what the number of threads is used by specific coroutines dispatcher?
我怎么知道 Dispatchers.IO 当前使用的线程数?
您可以使用 Android 分析器来监控线程。
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. ...
要监视线程,请在 Android Profiler 中使用 CPU 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.
发件人:
如您所见,here Dispatchers.IO
没有自己的线程池,它使用共享池。 Dispatchers.Default
使用相同的线程池。没有简单的方法来获取 Dispatchers.IO
当前使用的活动线程。但是您可以尝试在共享线程池中获取线程数。
公共线程池正在 CommonPool.kt
. It can create own pool or use ForkJoinPool 中创建。在池中创建的所有线程都有一个特定的名称。所以你可以通过名称找到共享池的所有活动线程。
val threads = Thread.getAllStackTraces().keys.filter {
it.name.startsWith("CommonPool") || it.name.startsWith("ForkJoinPool")
}
threads.size
我怎么知道 Dispatchers.IO 当前使用的线程数?
您可以使用 Android 分析器来监控线程。
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. ...
要监视线程,请在 Android Profiler 中使用 CPU 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.
发件人:
如您所见,here Dispatchers.IO
没有自己的线程池,它使用共享池。 Dispatchers.Default
使用相同的线程池。没有简单的方法来获取 Dispatchers.IO
当前使用的活动线程。但是您可以尝试在共享线程池中获取线程数。
公共线程池正在 CommonPool.kt
. It can create own pool or use ForkJoinPool 中创建。在池中创建的所有线程都有一个特定的名称。所以你可以通过名称找到共享池的所有活动线程。
val threads = Thread.getAllStackTraces().keys.filter {
it.name.startsWith("CommonPool") || it.name.startsWith("ForkJoinPool")
}
threads.size