Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/ThreadMXBean/AllThreadIds.java
38821 views
/*1* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 453053826* @summary Basic unit test of ThreadMXBean.getAllThreadIds()27* @author Alexei Guibadoulline and Mandy Chung28*29* @run main/othervm AllThreadIds30*/3132import java.lang.management.*;33import java.util.concurrent.Phaser;3435public class AllThreadIds {36final static int DAEMON_THREADS = 20;37final static int USER_THREADS = 5;38final static int ALL_THREADS = DAEMON_THREADS + USER_THREADS;39private static final boolean live[] = new boolean[ALL_THREADS];40private static final Thread allThreads[] = new Thread[ALL_THREADS];41private static final ThreadMXBean mbean = ManagementFactory.getThreadMXBean();42private static boolean testFailed = false;43private static boolean trace = false;4445private static long prevTotalThreadCount = 0;46private static int prevLiveThreadCount = 0;47private static int prevPeakThreadCount = 0;48private static long curTotalThreadCount = 0;49private static int curLiveThreadCount = 0;50private static int curPeakThreadCount = 0;5152private static final Phaser startupCheck = new Phaser(ALL_THREADS + 1);5354private static void printThreadList() {55if (!trace) return;5657long[] list = mbean.getAllThreadIds();58for (int i = 1; i <= list.length; i++) {59System.out.println(i + ": Thread id = " + list[i-1]);60}61for (int i = 0; i < ALL_THREADS; i++) {62Thread t = allThreads[i];63System.out.println(t.getName() + " Id = " + t.getId() +64" die = " + live[i] +65" alive = " + t.isAlive());66}67}6869private static void fail(String msg) {70trace = true;71printThreadList();72throw new RuntimeException(msg);73}7475private static void checkThreadCount(int numNewThreads,76int numTerminatedThreads)77throws Exception {78prevTotalThreadCount = curTotalThreadCount;79prevLiveThreadCount = curLiveThreadCount;80prevPeakThreadCount = curPeakThreadCount;81curTotalThreadCount = mbean.getTotalStartedThreadCount();82curLiveThreadCount = mbean.getThreadCount();83curPeakThreadCount = mbean.getPeakThreadCount();8485if ((curLiveThreadCount - prevLiveThreadCount) !=86(numNewThreads - numTerminatedThreads)) {87fail("Unexpected number of live threads: " +88" Prev live = " + prevLiveThreadCount +89" Current live = " + curLiveThreadCount +90" Threads added = " + numNewThreads +91" Threads terminated = " + numTerminatedThreads);92}93if (curPeakThreadCount - prevPeakThreadCount != numNewThreads) {94fail("Unexpected number of peak threads: " +95" Prev peak = " + prevPeakThreadCount +96" Current peak = " + curPeakThreadCount +97" Threads added = " + numNewThreads);98}99if (curTotalThreadCount - prevTotalThreadCount != numNewThreads) {100fail("Unexpected number of total threads: " +101" Prev Total = " + prevTotalThreadCount +102" Current Total = " + curTotalThreadCount +103" Threads added = " + numNewThreads);104}105long[] list = mbean.getAllThreadIds();106if (list.length != curLiveThreadCount) {107fail("Array length returned by " +108"getAllThreadIds() = " + list.length +109" not matched count = " + curLiveThreadCount);110}111}112113public static void main(String args[]) throws Exception {114if (args.length > 0 && args[0].equals("trace")) {115trace = true;116}117118curTotalThreadCount = mbean.getTotalStartedThreadCount();119curLiveThreadCount = mbean.getThreadCount();120curPeakThreadCount = mbean.getPeakThreadCount();121checkThreadCount(0, 0);122123// Start all threads and wait to be sure they all are alive124for (int i = 0; i < ALL_THREADS; i++) {125setLive(i, true);126allThreads[i] = new MyThread(i);127allThreads[i].setDaemon(i < DAEMON_THREADS);128allThreads[i].start();129}130// wait until all threads are started.131startupCheck.arriveAndAwaitAdvance();132133checkThreadCount(ALL_THREADS, 0);134printThreadList();135136// Check mbean now. All threads must appear in getAllThreadIds() list137long[] list = mbean.getAllThreadIds();138139for (int i = 0; i < ALL_THREADS; i++) {140long expectedId = allThreads[i].getId();141boolean found = false;142143if (trace) {144System.out.print("Looking for thread with id " + expectedId);145}146for (int j = 0; j < list.length; j++) {147if (expectedId == list[j]) {148found = true;149break;150}151}152153if (!found) {154testFailed = true;155}156if (trace) {157if (!found) {158System.out.print(". TEST FAILED.");159}160System.out.println();161}162}163if (trace) {164System.out.println();165}166167// Stop daemon threads, wait to be sure they all are dead, and check168// that they disappeared from getAllThreadIds() list169for (int i = 0; i < DAEMON_THREADS; i++) {170setLive(i, false);171}172173// make sure the daemon threads are completely dead174joinDaemonThreads();175176// and check the reported thread count177checkThreadCount(0, DAEMON_THREADS);178179// Check mbean now180list = mbean.getAllThreadIds();181182for (int i = 0; i < ALL_THREADS; i++) {183long expectedId = allThreads[i].getId();184boolean found = false;185boolean alive = (i >= DAEMON_THREADS);186187if (trace) {188System.out.print("Looking for thread with id " + expectedId +189(alive ? " expected alive." : " expected terminated."));190}191for (int j = 0; j < list.length; j++) {192if (expectedId == list[j]) {193found = true;194break;195}196}197198if (alive != found) {199testFailed = true;200}201if (trace) {202if (alive != found) {203System.out.println(" TEST FAILED.");204} else {205System.out.println();206}207}208}209210// Stop all threads and wait to be sure they all are dead211for (int i = DAEMON_THREADS; i < ALL_THREADS; i++) {212setLive(i, false);213}214215// make sure the non-daemon threads are completely dead216joinNonDaemonThreads();217218// and check the thread count219checkThreadCount(0, ALL_THREADS - DAEMON_THREADS);220221if (testFailed)222throw new RuntimeException("TEST FAILED.");223224System.out.println("Test passed.");225}226227private static void joinDaemonThreads() throws InterruptedException {228for (int i = 0; i < DAEMON_THREADS; i++) {229allThreads[i].join();230}231}232233private static void joinNonDaemonThreads() throws InterruptedException {234for (int i = DAEMON_THREADS; i < ALL_THREADS; i++) {235allThreads[i].join();236}237}238239private static void setLive(int i, boolean val) {240synchronized(live) {241live[i] = val;242}243}244245private static boolean isLive(int i) {246synchronized(live) {247return live[i];248}249}250251// The MyThread thread lives as long as correspondent live[i] value is true252private static class MyThread extends Thread {253int id;254255MyThread(int id) {256this.id = id;257}258259public void run() {260// signal started261startupCheck.arrive();262while (isLive(id)) {263try {264sleep(100);265} catch (InterruptedException e) {266System.out.println("Unexpected exception is thrown.");267e.printStackTrace(System.out);268testFailed = true;269}270}271}272}273}274275276