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

        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==============================================");

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

posted on 2017-12-16 18:11  albert1017  阅读(5285)  评论(0编辑  收藏  举报

导航