Android打印当前所有线程及对应栈信息

时间:2021-06-17 02:55:01
        Map<Thread, StackTraceElement[]> threadMap = Thread.getAllStackTraces();
Log.e("albertThreadDebug","all start==============================================");
for (Map.Entry<Thread, StackTraceElement[]> entry : threadMap.entrySet()) {
Thread thread = entry.getKey();
StackTraceElement[] stackElements = entry.getValue();
Log.e("albertThreadDebug","name:"+thread.getName()+" id:"+thread.getId()+" thread:"+thread.getPriority()+" begin==========");
for (int i = 0; i < stackElements.length; i++) {
StringBuilder stringBuilder = new StringBuilder(" ");
stringBuilder.append(stackElements[i].getClassName()+".")
.append(stackElements[i].getMethodName()+"(")
.append(stackElements[i].getFileName()+":")
.append(stackElements[i].getLineNumber()+")");
Log.e("albertThreadDebug",stringBuilder.toString());
}
Log.e("albertThreadDebug","name:"+thread.getName()+" id:"+thread.getId()+" thread:"+thread.getPriority()+" end==========");
}
Log.e("albertThreadDebug","all end==============================================");

这对调试死锁及各种线程阻塞比较有用。