Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/ThreadCounts.java
41152 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 mbean.getThreadCount()27* mbean.getTotalStartedThreadCount()28* mbean.getPeakThreadCount()29* mbean.getDaemonThreadCount()30* @author Alexei Guibadoulline31*32* @run main ThreadCounts33*/3435import java.lang.management.*;3637public class ThreadCounts {38static final int DAEMON_THREADS = 21;39static final int USER_THREADS_1 = 11;40static final int USER_THREADS_2 = 9;41static final int USER_THREADS = USER_THREADS_1 + USER_THREADS_2;42static final int ALL_THREADS = DAEMON_THREADS + USER_THREADS;43private static volatile boolean live[] = new boolean[ALL_THREADS];44private ThreadMXBean mbean = ManagementFactory.getThreadMXBean();45private static boolean testFailed = false;4647// barrier for threads communication48private static Barrier barrier = new Barrier(DAEMON_THREADS);4950public static void main(String argv[]) {51ThreadCounts test = new ThreadCounts();52Thread allThreads[] = new Thread[ALL_THREADS];5354// Start DAEMON_THREADS threads and wait to be sure they all are alive55barrier.set(DAEMON_THREADS);56for (int i = 0; i < DAEMON_THREADS; i++) {57live[i] = true;58allThreads[i] = new MyThread(i);59allThreads[i].setDaemon(true);60allThreads[i].start();61}62// wait until all threads have started.63barrier.await();646566System.out.println("Number of daemon threads added = " +67DAEMON_THREADS);68// Check mbean now69if ( (!test.checkCount (DAEMON_THREADS)) ||70(!test.checkCreated(DAEMON_THREADS)) ||71(!test.checkPeak (DAEMON_THREADS)) ||72(!test.checkDaemon (DAEMON_THREADS))73)74testFailed = true;7576// Start USER_THREADS_1 threads and wait to be sure they all are alive77barrier.set(USER_THREADS_1);78for (int i = DAEMON_THREADS; i < DAEMON_THREADS + USER_THREADS_1; i++) {79live[i] = true;80allThreads[i] = new MyThread(i);81allThreads[i].setDaemon(false);82allThreads[i].start();83}84// wait until user1 threads have started.85barrier.await();8687System.out.println("Number of threads added = " +88USER_THREADS_1);89// Check mbean now90if ( (!test.checkCount (DAEMON_THREADS + USER_THREADS_1)) ||91(!test.checkCreated(DAEMON_THREADS + USER_THREADS_1)) ||92(!test.checkPeak (DAEMON_THREADS + USER_THREADS_1)) ||93(!test.checkDaemon (DAEMON_THREADS))94)95testFailed = true;9697// Stop daemon threads and wait to be sure they all are dead98barrier.set(DAEMON_THREADS);99for (int i = 0; i < DAEMON_THREADS; i++) {100live[i] = false;101}102// wait until daemon threads terminated.103barrier.await();104105System.out.println("Daemon threads terminated.");106// Check mbean now107if ( (!test.checkCount (USER_THREADS_1)) ||108(!test.checkCreated(DAEMON_THREADS + USER_THREADS_1)) ||109(!test.checkPeak (DAEMON_THREADS + USER_THREADS_1)) ||110(!test.checkDaemon (0))111)112testFailed = true;113114// Start USER_THREADS_2 threads and wait to be sure they all are alive115barrier.set(USER_THREADS_2);116for (int i = DAEMON_THREADS + USER_THREADS_1; i < ALL_THREADS; i++) {117live[i] = true;118allThreads[i] = new MyThread(i);119allThreads[i].setDaemon(false);120allThreads[i].start();121}122// wait until user2 threads have started.123barrier.await();124125System.out.println("Number of threads added = " +126USER_THREADS_2);127// Check mbean now128if ( (!test.checkCount (USER_THREADS_1 + USER_THREADS_2)) ||129(!test.checkCreated(ALL_THREADS)) ||130(!test.checkPeak (DAEMON_THREADS + USER_THREADS_1)) ||131(!test.checkDaemon (0))132)133testFailed = true;134135// Stop user1 threads and wait to be sure they all are dead136barrier.set(USER_THREADS_1);137for (int i = DAEMON_THREADS; i < DAEMON_THREADS + USER_THREADS_1; i++) {138live[i] = false;139}140// wait until user1 threads terminated.141barrier.await();142143System.out.println("Number of threads terminated = " +144USER_THREADS_1);145// Check mbean now146if ( (!test.checkCount (USER_THREADS_2)) ||147(!test.checkCreated(ALL_THREADS)) ||148(!test.checkPeak (DAEMON_THREADS + USER_THREADS_1)) ||149(!test.checkDaemon (0))150)151testFailed = true;152153// Stop user2 threads and wait to be sure they all are dead154barrier.set(USER_THREADS_2);155for (int i = DAEMON_THREADS + USER_THREADS_1; i < ALL_THREADS; i++) {156live[i] = false;157}158// wait until user2 threads terminated.159barrier.await();160161System.out.println("Number of threads terminated = " +162USER_THREADS_2);163// Check mbean now164if ( (!test.checkCount (0)) ||165(!test.checkCreated(ALL_THREADS)) ||166(!test.checkPeak (DAEMON_THREADS + USER_THREADS_1)) ||167(!test.checkDaemon (0))168)169testFailed = true;170171if (testFailed)172throw new RuntimeException("TEST FAILED.");173174System.out.println("Test passed.");175}176177// Nobody knows how many threads are in the JVM in the exact moment. The178// only thing to check for sure is minimal number of threads alive (or179// created) in the application180private boolean checkCount(long min) {181long result = mbean.getThreadCount();182183if (result < min) {184System.err.println("TEST FAILED: " +185"Minimal number of live threads is " +186min +187". ThreadMXBean.getThreadCount() returned " +188result);189return false;190}191return true;192}193194private boolean checkCreated(long min) {195long result = mbean.getTotalStartedThreadCount();196197if (result < min) {198System.err.println("TEST FAILED: " +199"Minimal number of created threads is " +200min +201". ThreadMXBean.getTotalStartedThreadCount() "+202"returned " + result);203return false;204}205return true;206}207208private boolean checkPeak(long min) {209long result = mbean.getPeakThreadCount();210211if (result < min) {212System.err.println("TEST FAILED: " +213"Minimal peak thread count is " +214min +215". ThreadMXBean.getPeakThreadCount() "+216"returned " + result);217return false;218}219return true;220}221222private boolean checkDaemon(long min) {223long result = mbean.getDaemonThreadCount();224225if (result < min) {226System.err.println("TEST FAILED: " +227"Minimal number of daemon thread count is " +228min +229"ThreadMXBean.getDaemonThreadCount() returned "230+ result);231return false;232}233return true;234}235236// The MyThread thread lives as long as correspondent live[i] value is true237private static class MyThread extends Thread {238int id;239240MyThread(int id) {241this.id = id;242}243244public void run() {245// signal started246barrier.signal();247while (live[id]) {248try {249sleep(100);250} catch (InterruptedException e) {251System.out.println("Unexpected exception is thrown.");252e.printStackTrace(System.out);253testFailed = true;254}255}256// signal about to exit257barrier.signal();258}259}260}261262263