File: ThreadLists.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (147 lines) | stat: -rw-r--r-- 6,259 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (c) 2004, 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 5047639 8132785
 * @summary Check that the "java-level" APIs provide a consistent view of
 *          the thread list
 * @comment Must run in othervm mode to avoid interference from other tests.
 * @run main/othervm ThreadLists
 */
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class ThreadLists {

    // Thread names permitted to appear during test:
    public static final String [] permittedThreadNames = { "ForkJoinPool", "JVMCI" };

    public static boolean isPermittedNewThread(String name) {
        for (String s : permittedThreadNames) {
            if (name.contains(s)) {
                return true;
            }
        }
        return false;
    }

    public static void main(String args[]) {

        // Bug id : JDK-8151797
        // Use a lambda expression so that call-site cleaner thread is started
        Runnable printLambda = () -> {System.out.println("Starting Test");};
        printLambda.run();

        // get top-level thread group
        ThreadGroup top = Thread.currentThread().getThreadGroup();
        ThreadGroup parent;
        do {
            parent = top.getParent();
            if (parent != null) top = parent;
        } while (parent != null);

        // get the thread count
        int tgActiveCount = top.activeCount();

        // Now enumerate to see if we find any extras yet.
        // Ensure array is big enough for a few extras.
        Thread[] tgThreads = new Thread[tgActiveCount * 2];
        int tgNewCount = top.enumerate(tgThreads);
        Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();

        if (tgNewCount != tgActiveCount) {
            System.out.println("Found different Thread Group thread count after enumeration: tgActiveCount="
                               + tgActiveCount + " enumerated=" + tgNewCount);
        }
        if (tgNewCount != stackTraces.size()) {
            System.out.println("Found difference in counts: thread group new count="
                               + tgNewCount + " stackTraces.size()=" + stackTraces.size());
        }
        System.out.println("Initial set of enumerated threads:");
        for (int i = 0; i < tgNewCount; i++) {
            System.out.println(" - Thread: " + tgThreads[i].getName());
        }

        // Get Threads from MXBean.  Retry to ensure count and id count match.
        ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
        int threadCountBean = 0;
        long[] threadIdsBean = null;
        do {
            System.out.println("Gathering Thread info from MXBean...");
            threadCountBean = threadBean.getThreadCount();
            threadIdsBean = threadBean.getAllThreadIds();
        } while (threadCountBean != threadIdsBean.length);

        System.out.println("ThreadGroup:              " + tgActiveCount + " active thread(s)");
        System.out.println("Thread.getAllStackTraces: " + stackTraces.size() + " stack trace(s) returned");
        System.out.println("ThreadMXBean:             " + threadCountBean + " live threads(s)");
        System.out.println("ThreadMXBean:             " + threadIdsBean.length + " thread Id(s)");

        if (threadIdsBean.length > tgActiveCount) {
            // Find the new Threads: some Thead names are permitted to appear: ignore them.
            Set<Long> seenTids = new TreeSet<>();
            for (Thread t : stackTraces.keySet()) {
                if (t != null) {
                    seenTids.add(t.getId());
                }
            }
            for (long tid : threadIdsBean) {
                if (!seenTids.contains(tid)) {
                    // New Thread from MBean, compared to Thread Group:
                    ThreadInfo threadInfo = threadBean.getThreadInfo(tid);
                    if (threadInfo != null && isPermittedNewThread(threadInfo.getThreadName())) {
                        System.out.print("New thread permitted: " + threadInfo);
                        threadCountBean--;
                    }
                }
            }
        }

        // check results are consistent
        boolean failed = false;
        if (tgActiveCount != stackTraces.size()) failed = true;
        if (tgActiveCount != threadCountBean) failed = true;
        // We know threadCountBean == threadIdsBean.length

        if (failed) {
            System.out.println("Failed.");
            System.out.println("Set of Threads from getAllStackTraces:");
            for (Thread t : stackTraces.keySet()) {
                System.out.println(" - Thread: " +
                                   (t != null ? t.getName() : "null!"));
            }
            System.out.println("Set of Thread IDs from MXBean:");
            for (long tid : threadIdsBean) {
                System.out.print(tid + " ");
                ThreadInfo threadInfo = threadBean.getThreadInfo(tid);
                System.out.println(threadInfo != null ? threadInfo.getThreadName() : "");
            }
            throw new RuntimeException("inconsistent results");
        }
    }
}