通过adb命令
获取pid
ps -A | grep 应用包名
获取进程信息
adb shell cat /proc/[获取pid]/status
当然也可以使用ps命令查看
ps -T -p [pid]
top自动更新线程信息
top -H -p [pid]
通过代码获取
object ThreadUtils {
fun getAllThreadSize(): Int {
var size = 0
File("/proc/self/status").reader().forEachLine { line ->
if (line.startsWith("Threads")) {
//Threads: 1
val regex = "\\d+".toRegex()
val matchResult = regex.find(line)
size = matchResult?.value?.toInt() ?: 0
}
}
return size
}
fun getAllJavaThreads(): List<Thread> {
var system = Thread.currentThread().threadGroup
while (system?.parent != null) {
system = system.parent
}
val count = system?.activeCount() ?: 0
//see java.lang.Thread.getAllStackTraces
val threads = arrayOfNulls<Thread>(count + count / 2)
system?.enumerate(threads)
val list = mutableListOf<Thread>()
threads.forEach {
it?.let { it1 -> list.add(it1) }
}
return list
}
}