Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/DisableTest.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 495697826* @summary The capability is disabled regardless of number of times27* it was enabled.28* @author Mandy Chung29*/3031import java.lang.management.ThreadMXBean;32import java.lang.management.ManagementFactory;3334public class DisableTest {35private static ThreadMXBean tm = ManagementFactory.getThreadMXBean();3637public static void main(String args[]) throws Exception {38try {39testThreadContentionMonitoring();40testThreadCpuMonitoring();41} finally {42// restore the default43if (tm.isThreadContentionMonitoringSupported()) {44tm.setThreadContentionMonitoringEnabled(false);45}46if (tm.isThreadCpuTimeSupported()) {47tm.setThreadCpuTimeEnabled(false);48}49}5051System.out.println("Test passed.");52}5354private static void testThreadContentionMonitoring()55throws Exception {56if (!tm.isThreadContentionMonitoringSupported()) {57System.out.println("JVM does not supports thread contention monitoring");58return;59}6061// Default is false.62tm.setThreadContentionMonitoringEnabled(false);63tm.setThreadContentionMonitoringEnabled(false);6465// check if disabled66if (tm.isThreadContentionMonitoringEnabled()) {67throw new RuntimeException("TEST FAILED: " +68"Expected thread contention monitoring to be disabled");69}7071tm.setThreadContentionMonitoringEnabled(true);72// check if enabled73if (!tm.isThreadContentionMonitoringEnabled()) {74throw new RuntimeException("TEST FAILED: " +75"Expected thread contention monitoring to be enabled");76}77}7879private static void testThreadCpuMonitoring()80throws Exception {81if (!tm.isThreadCpuTimeSupported()) {82System.out.println("JVM does not support thread CPU time monitoring");83return;84}8586if (tm.isThreadCpuTimeEnabled()) {87tm.setThreadCpuTimeEnabled(false);88}8990// check if disabled91if (tm.isThreadCpuTimeEnabled()) {92throw new RuntimeException("TEST FAILED: " +93"Expected thread CPU time monitoring to be disabled");94}9596tm.setThreadCpuTimeEnabled(false);97tm.setThreadCpuTimeEnabled(false);9899if (tm.isThreadCpuTimeEnabled()) {100throw new RuntimeException("TEST FAILED: " +101"Expected thread CPU time monitoring to be disabled");102}103104tm.setThreadCpuTimeEnabled(true);105if (!tm.isThreadCpuTimeEnabled()) {106throw new RuntimeException("TEST FAILED: " +107"Expected thread CPU time monitoring to be disabled");108}109}110111}112113114