Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/EnableTest.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 of27* ThreadMXBean.setThreadContentionMonitoringEnabled()28* and ThreadMXBean.setThreadCpuTimeEnabled().29* @author Mandy Chung30*31* @run main EnableTest32*/3334import java.lang.management.*;3536public class EnableTest {37private static ThreadMXBean tm = ManagementFactory.getThreadMXBean();3839private static void checkThreadContentionMonitoring(boolean expectedValue)40throws Exception {41boolean value = tm.isThreadContentionMonitoringEnabled();42if (value != expectedValue) {43throw new RuntimeException("TEST FAILED: " +44"isThreadContentionMonitoringEnabled() returns " + value +45" but expected to be " + expectedValue);46}4748}4950private static void testThreadContentionMonitoring()51throws Exception {52if (!tm.isThreadContentionMonitoringSupported()) return;5354// Test setThreadContentionMonitoringEnabled()55checkThreadContentionMonitoring(false);5657// Check enabled once58tm.setThreadContentionMonitoringEnabled(true);59checkThreadContentionMonitoring(true);6061// Enable it two more times62tm.setThreadContentionMonitoringEnabled(true);63checkThreadContentionMonitoring(true);6465tm.setThreadContentionMonitoringEnabled(true);6667// Disable once will globally disable the thread contention monitoring68tm.setThreadContentionMonitoringEnabled(false);69checkThreadContentionMonitoring(false);7071tm.setThreadContentionMonitoringEnabled(false);72checkThreadContentionMonitoring(false);7374tm.setThreadContentionMonitoringEnabled(true);75checkThreadContentionMonitoring(true);76}7778private static void checkThreadCpuTime(boolean expectedValue)79throws Exception {80boolean value = tm.isThreadCpuTimeEnabled();81if (value != expectedValue) {82throw new RuntimeException("TEST FAILED: " +83"isThreadCpuTimeEnabled() returns " + value +84" but expected to be " + expectedValue);85}86}8788private static void testThreadCpuTime()89throws Exception {90if (!tm.isThreadCpuTimeSupported()) return;9192// Test setThreadCpuTimeEnabled()93if (!tm.isThreadCpuTimeEnabled()) {94tm.setThreadCpuTimeEnabled(true);95checkThreadCpuTime(true);96}9798tm.setThreadCpuTimeEnabled(false);99checkThreadCpuTime(false);100101tm.setThreadCpuTimeEnabled(true);102checkThreadCpuTime(true);103104tm.setThreadCpuTimeEnabled(true);105checkThreadCpuTime(true);106107tm.setThreadCpuTimeEnabled(true);108109// disable globally110tm.setThreadCpuTimeEnabled(false);111checkThreadCpuTime(false);112113tm.setThreadCpuTimeEnabled(false);114checkThreadCpuTime(false);115116tm.setThreadCpuTimeEnabled(true);117checkThreadCpuTime(true);118}119120public static void main(String args[]) throws Exception {121try {122testThreadContentionMonitoring();123testThreadCpuTime();124} finally {125// restore the default126if (tm.isThreadContentionMonitoringSupported()) {127tm.setThreadContentionMonitoringEnabled(false);128}129if (tm.isThreadCpuTimeSupported()) {130tm.setThreadCpuTimeEnabled(false);131}132}133134135System.out.println("Test passed.");136}137}138139140