Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/ResetPeakThreadCount.java
41152 views
/*1* Copyright (c) 2003, 2021, 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.*;37import java.util.Iterator;38import java.util.LinkedList;39import java.util.List;4041public class ResetPeakThreadCount {42// initial number of new threads started43private static final int DAEMON_THREADS_1 = 80;4445// Terminate half of the threads started46private static final int TERMINATE_1 = 40;4748// start new threads but expected the peak unchanged49private static final int DAEMON_THREADS_2 = 20;5051// peak thread count reset before starting new threads52private static final int DAEMON_THREADS_3 = 20;5354// barrier for threads communication55private static final Barrier barrier = new Barrier(DAEMON_THREADS_1);5657private static final ThreadMXBean mbean = ManagementFactory.getThreadMXBean();58private static volatile boolean testFailed = false;5960private static final List<MyThread> threads = new LinkedList<>();61private static final Object liveSync = new Object();6263public static void main(String[] argv) throws Exception {64// System threads can be started/terminated during the test execution,65// and they affect resetPeakThreadCount result.66resetPeak();6768startThreads(DAEMON_THREADS_1);6970int beforeTerminate = checkPeakThreadCount(threads.size() + 1, -1); // + 1 for the current thread7172terminateThreads(TERMINATE_1);7374// the value should not decrease75int afterTerminate = checkPeakThreadCount(beforeTerminate, -1);7677startThreads(DAEMON_THREADS_2);7879// expected peak is unchanged80checkPeakThreadCount(-1, afterTerminate);8182// reset peak and ensure new threads increase the value83int beforeThreads3 = resetPeak();84startThreads(DAEMON_THREADS_3);8586checkPeakThreadCount(threads.size() + 1, -1); // + 1 for the current thread87checkPeakThreadCount(beforeThreads3, -1);8889if (testFailed) {90throw new RuntimeException("TEST FAILED.");91}9293System.out.println("Test passed");94}9596private static void startThreads(int count) throws InterruptedException {97// get current peak thread count98int peak1 = mbean.getPeakThreadCount();99100// Start threads and wait to be sure they all are alive101System.out.println("Starting " + count + " threads....");102barrier.set(count);103synchronized (liveSync) {104for (int i = 0; i < count; i++) {105MyThread newThread = new MyThread();106threads.add(newThread);107newThread.start();108}109}110// wait until all threads have started.111barrier.await();112113// get peak thread count after daemon threads have started114int peak2 = mbean.getPeakThreadCount();115116System.out.println(" Current = " + mbean.getThreadCount() +117" Peak before = " + peak1 + " after: " + peak2);118119int current = mbean.getThreadCount();120System.out.println(" Live thread count before returns " + current);121}122123private static void terminateThreads(int count) throws InterruptedException {124// get current peak thread count125int peak1 = mbean.getPeakThreadCount();126127// Stop daemon threads and wait to be sure they all are dead128System.out.println("Terminating " + count + " threads....");129barrier.set(count);130synchronized(liveSync) {131Iterator<MyThread> iter = threads.iterator();132for (int i = 0; i < count; i++) {133MyThread thread = iter.next();134thread.live = false;135}136liveSync.notifyAll();137}138// wait until daemon threads terminated.139barrier.await();140141// get peak thread count after daemon threads have terminated142int peak2 = mbean.getPeakThreadCount();143// assuming no system thread is added144if (peak2 != peak1) {145throw new RuntimeException("Current Peak = " + peak2 +146" Expected to be = previous peak = " + peak1);147}148149for (int i = 0; i < count; i++) {150MyThread thread = threads.remove(0);151thread.join();152}153154int current = mbean.getThreadCount();155System.out.println(" Live thread count before returns " + current);156}157158// Returns peak thread value after reset.159private static int resetPeak() {160int peak3 = mbean.getPeakThreadCount();161int current = mbean.getThreadCount();162163// Reset peak thread count164mbean.resetPeakThreadCount();165166int afterResetPeak = mbean.getPeakThreadCount();167int afterResetCurrent = mbean.getThreadCount();168System.out.println("Reset peak before = " + peak3 +169" current = " + current +170" after reset peak = " + afterResetPeak +171" current = " + afterResetCurrent);172return afterResetPeak;173}174175private static void fail(String msg) {176ThreadDump.threadDump();177throw new RuntimeException(msg);178}179180private static int checkPeakThreadCount(int min, int max) {181int value = mbean.getPeakThreadCount();182if (min > 0 && value < min) {183fail("***** Unexpected thread count: " + value + ", minimum expected " + min + " *****");184}185if (max > 0 && value > max) {186fail("***** Unexpected thread count: " + value + ", maximum expected " + max + " *****");187}188return value;189}190191192// The MyThread thread lives as long as correspondent its live value is true.193private static class MyThread extends Thread {194volatile boolean live;195196MyThread() {197live = true;198setDaemon(true);199}200201public void run() {202// signal started203barrier.signal();204synchronized(liveSync) {205while (live) {206try {207liveSync.wait(100);208} catch (InterruptedException e) {209System.out.println("Unexpected exception is thrown.");210e.printStackTrace(System.out);211testFailed = true;212}213}214}215// signal about to exit216barrier.signal();217}218}219220}221222223