Android prints all thread information currently running

Android prints all thread information currently running


During the running of the APP, in what way can the information of all the threads currently running be obtained?

Go directly to the code:

    private final String TAG = "budaye";
    public void printAllThreadInfo() {
        Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
        Log.d(TAG, "线程总数 = " + allStackTraces.size());
        for (Map.Entry<Thread, StackTraceElement[]> stackTrace : allStackTraces.entrySet()) {
            Thread thread = stackTrace.getKey();
            Log.d(TAG, "线程:" + thread.getName() + ", id=" + thread.getId() + ", state=" + thread.getState());
            StackTraceElement[] stack = stackTrace.getValue();
            String strStackTrace = "堆栈:";
            for (StackTraceElement stackTraceElement : stack) {
                strStackTrace += stackTraceElement.toString() + "\n";
            }
            Log.d(TAG, strStackTrace);
        }
    }

Original technical articles, continuously updated, please pay attention to favorites~

Guess you like

Origin blog.csdn.net/u011578734/article/details/127329878