Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/ThreadMXBean/ResetPeakThreadCount.java
38821 views
/*1* Copyright (c) 2003, 2013, 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 4892507 8020875 802133526* @summary Basic Test for the following reset methods:27* - ThreadMXBean.resetPeakThreadCount()28* @author Mandy Chung29* @author Jaroslav Bachorik30*31* @build ResetPeakThreadCount32* @build ThreadDump33* @run main/othervm ResetPeakThreadCount34*/3536import java.lang.management.*;3738public class ResetPeakThreadCount {39// initial number of new threads started40private static final int DAEMON_THREADS_1 = 8;41private static final int EXPECTED_PEAK_DELTA_1 = 8;4243// Terminate half of the threads started44private static final int TERMINATE_1 = 4;4546// start new threads but expected the peak unchanged47private static final int DAEMON_THREADS_2 = 2;48private static final int EXPECTED_PEAK_DELTA_2 = 0;4950// peak thread count reset before starting new threads51private static final int DAEMON_THREADS_3 = 4;52private static final int EXPECTED_PEAK_DELTA_3 = 4;5354private static final int TERMINATE_2 = 8;5556private static final int TERMINATE_3 = 2;5758private static final int ALL_THREADS = DAEMON_THREADS_1 +59DAEMON_THREADS_2 + DAEMON_THREADS_3;60// barrier for threads communication61private static final Barrier barrier = new Barrier(DAEMON_THREADS_1);6263private static final Thread allThreads[] = new Thread[ALL_THREADS];64private static final boolean live[] = new boolean[ALL_THREADS];65private static final ThreadMXBean mbean = ManagementFactory.getThreadMXBean();66private static volatile boolean testFailed = false;6768public static void main(String[] argv) throws Exception {69// This test does not expect any threads to be created70// by the test harness after main() is invoked.71// The checkThreadCount() method is to produce more72// diagnostic information in case any unexpected test failure occur.73long previous = mbean.getThreadCount();74long current = previous;7576// reset the peak to start from a scratch77resetPeak(current);7879// start DAEMON_THREADS_1 number of threads80current = startThreads(0, DAEMON_THREADS_1, EXPECTED_PEAK_DELTA_1);8182checkThreadCount(previous, current, DAEMON_THREADS_1);83previous = current;8485// terminate TERMINATE_1 number of threads and reset peak86current = terminateThreads(0, TERMINATE_1);8788checkThreadCount(previous, current, TERMINATE_1 * -1);8990previous = current;9192// start DAEMON_THREADS_2 number of threads93// expected peak is unchanged94current = startThreads(DAEMON_THREADS_1, DAEMON_THREADS_2,95EXPECTED_PEAK_DELTA_2);9697checkThreadCount(previous, current, DAEMON_THREADS_2);98previous = current;99100// Reset the peak101resetPeak(current);102103// start DAEMON_THREADS_3 number of threads104current = startThreads(DAEMON_THREADS_1 + DAEMON_THREADS_2,105DAEMON_THREADS_3, EXPECTED_PEAK_DELTA_3);106107checkThreadCount(previous, current, DAEMON_THREADS_3);108previous = current;109110// terminate TERMINATE_2 number of threads and reset peak111current = terminateThreads(TERMINATE_1, TERMINATE_2);112113checkThreadCount(previous, current, TERMINATE_2 * -1);114previous = current;115116resetPeak(current);117118// terminate TERMINATE_3 number of threads and reset peak119current = terminateThreads(TERMINATE_1 + TERMINATE_2, TERMINATE_3);120121checkThreadCount(previous, current, TERMINATE_3 * -1);122resetPeak(current);123124if (testFailed)125throw new RuntimeException("TEST FAILED.");126127System.out.println("Test passed");128}129130private static long startThreads(int from, int count, int delta) throws InterruptedException {131// get current peak thread count132long peak1 = mbean.getPeakThreadCount();133long current = mbean.getThreadCount();134135// Start threads and wait to be sure they all are alive136System.out.println("Starting " + count + " threads....");137barrier.set(count);138synchronized(live) {139for (int i = from; i < (from + count); i++) {140live[i] = true;141allThreads[i] = new MyThread(i);142allThreads[i].setDaemon(true);143allThreads[i].start();144}145}146// wait until all threads have started.147barrier.await();148149// get peak thread count after daemon threads have started150long peak2 = mbean.getPeakThreadCount();151152System.out.println(" Current = " + mbean.getThreadCount() +153" Peak before = " + peak1 + " after: " + peak2);154155if (peak2 != (peak1 + delta)) {156throw new RuntimeException("Current Peak = " + peak2 +157" Expected to be == previous peak = " + peak1 + " + " +158delta);159}160// wait until the current thread count gets incremented161while (mbean.getThreadCount() < (current + count)) {162Thread.sleep(100);163}164current = mbean.getThreadCount();165System.out.println(" Live thread count before returns " + current);166return current;167}168169private static long terminateThreads(int from, int count) throws InterruptedException {170// get current peak thread count171long peak1 = mbean.getPeakThreadCount();172173// Stop daemon threads and wait to be sure they all are dead174System.out.println("Terminating " + count + " threads....");175barrier.set(count);176synchronized(live) {177for (int i = from; i < (from+count); i++) {178live[i] = false;179}180live.notifyAll();181}182// wait until daemon threads terminated.183barrier.await();184185// get peak thread count after daemon threads have terminated186long peak2 = mbean.getPeakThreadCount();187// assuming no system thread is added188if (peak2 != peak1) {189throw new RuntimeException("Current Peak = " + peak2 +190" Expected to be = previous peak = " + peak1);191}192193for (int i = from; i < (from+count); i++) {194allThreads[i].join();195}196197// there is a race in the counter update logic somewhere causing198// the thread counters go ff199// we need to give the terminated threads some extra time to really die200// JDK-8021335201Thread.sleep(500);202203long current = mbean.getThreadCount();204System.out.println(" Live thread count before returns " + current);205return current;206}207208private static void resetPeak(long expectedCount) {209long peak3 = mbean.getPeakThreadCount();210long current = mbean.getThreadCount();211212// Nightly testing showed some intermittent failure.213// Check here to get diagnostic information if some strange214// behavior occurs.215checkThreadCount(expectedCount, current, 0);216217// Reset peak thread count218mbean.resetPeakThreadCount();219220long afterResetPeak = mbean.getPeakThreadCount();221long afterResetCurrent = mbean.getThreadCount();222System.out.println("Reset peak before = " + peak3 +223" current = " + current +224" after reset peak = " + afterResetPeak +225" current = " + afterResetCurrent);226227if (afterResetPeak != current) {228throw new RuntimeException("Current Peak after reset = " +229afterResetPeak +230" Expected to be = current count = " + current);231}232}233234private static void checkThreadCount(long previous, long current, int expectedDelta) {235if (current != previous + expectedDelta) {236ThreadDump.threadDump();237throw new RuntimeException("***** Unexpected thread count:" +238" previous = " + previous +239" current = " + current +240" delta = " + expectedDelta + "*****");241}242}243244// The MyThread thread lives as long as correspondent live[i] value is true245private static class MyThread extends Thread {246int id;247248MyThread(int id) {249this.id = id;250}251252public void run() {253// signal started254barrier.signal();255synchronized(live) {256while (live[id]) {257try {258live.wait(100);259} catch (InterruptedException e) {260System.out.println("Unexpected exception is thrown.");261e.printStackTrace(System.out);262testFailed = true;263}264}265}266// signal about to exit267barrier.signal();268}269}270271}272273274