0N/A/*
2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4530538
0N/A * @summary Basic unit test of mbean.getThreadCount()
0N/A * mbean.getTotalStartedThreadCount()
0N/A * mbean.getPeakThreadCount()
0N/A * mbean.getDaemonThreadCount()
0N/A * @author Alexei Guibadoulline
0N/A *
0N/A * @run main ThreadCounts
0N/A */
0N/A
0N/Aimport java.lang.management.*;
0N/A
0N/Apublic class ThreadCounts {
0N/A final static int DAEMON_THREADS = 21;
0N/A final static int USER_THREADS_1 = 11;
0N/A final static int USER_THREADS_2 = 9;
0N/A final static int USER_THREADS = USER_THREADS_1 + USER_THREADS_2;
0N/A final static int ALL_THREADS = DAEMON_THREADS + USER_THREADS;
0N/A private static volatile boolean live[] = new boolean[ALL_THREADS];
0N/A private ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
0N/A private static boolean testFailed = false;
0N/A
0N/A // barrier for threads communication
0N/A private static Barrier barrier = new Barrier(DAEMON_THREADS);
0N/A
0N/A public static void main(String argv[]) {
0N/A ThreadCounts test = new ThreadCounts();
0N/A Thread allThreads[] = new Thread[ALL_THREADS];
0N/A
0N/A // Start DAEMON_THREADS threads and wait to be sure they all are alive
0N/A barrier.set(DAEMON_THREADS);
0N/A for (int i = 0; i < DAEMON_THREADS; i++) {
0N/A live[i] = true;
0N/A allThreads[i] = new MyThread(i);
0N/A allThreads[i].setDaemon(true);
0N/A allThreads[i].start();
0N/A }
0N/A // wait until all threads have started.
0N/A barrier.await();
0N/A
0N/A
0N/A System.out.println("Number of daemon threads added = " +
0N/A DAEMON_THREADS);
0N/A // Check mbean now
0N/A if ( (!test.checkCount (DAEMON_THREADS)) ||
0N/A (!test.checkCreated(DAEMON_THREADS)) ||
0N/A (!test.checkPeak (DAEMON_THREADS)) ||
0N/A (!test.checkDaemon (DAEMON_THREADS))
0N/A )
0N/A testFailed = true;
0N/A
0N/A // Start USER_THREADS_1 threads and wait to be sure they all are alive
0N/A barrier.set(USER_THREADS_1);
0N/A for (int i = DAEMON_THREADS; i < DAEMON_THREADS + USER_THREADS_1; i++) {
0N/A live[i] = true;
0N/A allThreads[i] = new MyThread(i);
0N/A allThreads[i].setDaemon(false);
0N/A allThreads[i].start();
0N/A }
0N/A // wait until user1 threads have started.
0N/A barrier.await();
0N/A
0N/A System.out.println("Number of threads added = " +
0N/A USER_THREADS_1);
0N/A // Check mbean now
0N/A if ( (!test.checkCount (DAEMON_THREADS + USER_THREADS_1)) ||
0N/A (!test.checkCreated(DAEMON_THREADS + USER_THREADS_1)) ||
0N/A (!test.checkPeak (DAEMON_THREADS + USER_THREADS_1)) ||
0N/A (!test.checkDaemon (DAEMON_THREADS))
0N/A )
0N/A testFailed = true;
0N/A
0N/A // Stop daemon threads and wait to be sure they all are dead
0N/A barrier.set(DAEMON_THREADS);
0N/A for (int i = 0; i < DAEMON_THREADS; i++) {
0N/A live[i] = false;
0N/A }
0N/A // wait until daemon threads terminated.
0N/A barrier.await();
0N/A
0N/A System.out.println("Daemon threads terminated.");
0N/A // Check mbean now
0N/A if ( (!test.checkCount (USER_THREADS_1)) ||
0N/A (!test.checkCreated(DAEMON_THREADS + USER_THREADS_1)) ||
0N/A (!test.checkPeak (DAEMON_THREADS + USER_THREADS_1)) ||
0N/A (!test.checkDaemon (0))
0N/A )
0N/A testFailed = true;
0N/A
0N/A // Start USER_THREADS_2 threads and wait to be sure they all are alive
0N/A barrier.set(USER_THREADS_2);
0N/A for (int i = DAEMON_THREADS + USER_THREADS_1; i < ALL_THREADS; i++) {
0N/A live[i] = true;
0N/A allThreads[i] = new MyThread(i);
0N/A allThreads[i].setDaemon(false);
0N/A allThreads[i].start();
0N/A }
0N/A // wait until user2 threads have started.
0N/A barrier.await();
0N/A
0N/A System.out.println("Number of threads added = " +
0N/A USER_THREADS_2);
0N/A // Check mbean now
0N/A if ( (!test.checkCount (USER_THREADS_1 + USER_THREADS_2)) ||
0N/A (!test.checkCreated(ALL_THREADS)) ||
0N/A (!test.checkPeak (DAEMON_THREADS + USER_THREADS_1)) ||
0N/A (!test.checkDaemon (0))
0N/A )
0N/A testFailed = true;
0N/A
0N/A // Stop user1 threads and wait to be sure they all are dead
0N/A barrier.set(USER_THREADS_1);
0N/A for (int i = DAEMON_THREADS; i < DAEMON_THREADS + USER_THREADS_1; i++) {
0N/A live[i] = false;
0N/A }
0N/A // wait until user1 threads terminated.
0N/A barrier.await();
0N/A
0N/A System.out.println("Number of threads terminated = " +
0N/A USER_THREADS_1);
0N/A // Check mbean now
0N/A if ( (!test.checkCount (USER_THREADS_2)) ||
0N/A (!test.checkCreated(ALL_THREADS)) ||
0N/A (!test.checkPeak (DAEMON_THREADS + USER_THREADS_1)) ||
0N/A (!test.checkDaemon (0))
0N/A )
0N/A testFailed = true;
0N/A
0N/A // Stop user2 threads and wait to be sure they all are dead
0N/A barrier.set(USER_THREADS_2);
0N/A for (int i = DAEMON_THREADS + USER_THREADS_1; i < ALL_THREADS; i++) {
0N/A live[i] = false;
0N/A }
0N/A // wait until user2 threads terminated.
0N/A barrier.await();
0N/A
0N/A System.out.println("Number of threads terminated = " +
0N/A USER_THREADS_2);
0N/A // Check mbean now
0N/A if ( (!test.checkCount (0)) ||
0N/A (!test.checkCreated(ALL_THREADS)) ||
0N/A (!test.checkPeak (DAEMON_THREADS + USER_THREADS_1)) ||
0N/A (!test.checkDaemon (0))
0N/A )
0N/A testFailed = true;
0N/A
0N/A if (testFailed)
0N/A throw new RuntimeException("TEST FAILED.");
0N/A
0N/A System.out.println("Test passed.");
0N/A }
0N/A
0N/A // Nobody knows how many threads are in the JVM in the exact moment. The
0N/A // only thing to check for sure is minimal number of threads alive (or
0N/A // created) in the application
0N/A private boolean checkCount(long min) {
0N/A long result = mbean.getThreadCount();
0N/A
0N/A if (result < min) {
0N/A System.err.println("TEST FAILED: " +
0N/A "Minimal number of live threads is " +
0N/A min +
0N/A ". ThreadMXBean.getThreadCount() returned " +
0N/A result);
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A private boolean checkCreated(long min) {
0N/A long result = mbean.getTotalStartedThreadCount();
0N/A
0N/A if (result < min) {
0N/A System.err.println("TEST FAILED: " +
0N/A "Minimal number of created threads is " +
0N/A min +
0N/A ". ThreadMXBean.getTotalStartedThreadCount() "+
0N/A "returned " + result);
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A private boolean checkPeak(long min) {
0N/A long result = mbean.getPeakThreadCount();
0N/A
0N/A if (result < min) {
0N/A System.err.println("TEST FAILED: " +
0N/A "Minimal peak thread count is " +
0N/A min +
0N/A ". ThreadMXBean.getPeakThreadCount() "+
0N/A "returned " + result);
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A private boolean checkDaemon(long min) {
0N/A long result = mbean.getDaemonThreadCount();
0N/A
0N/A if (result < min) {
0N/A System.err.println("TEST FAILED: " +
0N/A "Minimal number of daemon thread count is " +
0N/A min +
0N/A "ThreadMXBean.getDaemonThreadCount() returned "
0N/A + result);
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A // The MyThread thread lives as long as correspondent live[i] value is true
0N/A private static class MyThread extends Thread {
0N/A int id;
0N/A
0N/A MyThread(int id) {
0N/A this.id = id;
0N/A }
0N/A
0N/A public void run() {
0N/A // signal started
0N/A barrier.signal();
0N/A while (live[id]) {
0N/A try {
0N/A sleep(100);
0N/A } catch (InterruptedException e) {
0N/A System.out.println("Unexpected exception is thrown.");
0N/A e.printStackTrace(System.out);
0N/A testFailed = true;
0N/A }
0N/A }
0N/A // signal about to exit
0N/A barrier.signal();
0N/A }
0N/A }
0N/A}