1 /* 2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4956978 27 * @summary The capability is disabled regardless of number of times 28 * it was enabled. 29 * @author Mandy Chung 30 * @modules java.management 31 */ 32 33 import java.lang.management.ThreadMXBean; 34 import java.lang.management.ManagementFactory; 35 36 public class DisableTest { 37 private static ThreadMXBean tm = ManagementFactory.getThreadMXBean(); 38 39 public static void main(String args[]) throws Exception { 40 try { 41 testThreadContentionMonitoring(); 42 testThreadCpuMonitoring(); 43 } finally { 44 // restore the default 45 if (tm.isThreadContentionMonitoringSupported()) { 46 tm.setThreadContentionMonitoringEnabled(false); 47 } 48 if (tm.isThreadCpuTimeSupported()) { 49 tm.setThreadCpuTimeEnabled(false); 50 } 51 } 52 53 System.out.println("Test passed."); 54 } 55 56 private static void testThreadContentionMonitoring() 57 throws Exception { 58 if (!tm.isThreadContentionMonitoringSupported()) { 59 System.out.println("JVM does not supports thread contention monitoring"); 60 return; 61 } 62 63 // Default is false. 64 tm.setThreadContentionMonitoringEnabled(false); 65 tm.setThreadContentionMonitoringEnabled(false); 66 67 // check if disabled 68 if (tm.isThreadContentionMonitoringEnabled()) { 69 throw new RuntimeException("TEST FAILED: " + 70 "Expected thread contention monitoring to be disabled"); 71 } 72 73 tm.setThreadContentionMonitoringEnabled(true); 74 // check if enabled 75 if (!tm.isThreadContentionMonitoringEnabled()) { 76 throw new RuntimeException("TEST FAILED: " + 77 "Expected thread contention monitoring to be enabled"); 78 } 79 } 80 81 private static void testThreadCpuMonitoring() 82 throws Exception { 83 if (!tm.isThreadCpuTimeSupported()) { 84 System.out.println("JVM does not support thread CPU time monitoring"); 85 return; 86 } 87 88 if (tm.isThreadCpuTimeEnabled()) { 89 tm.setThreadCpuTimeEnabled(false); 90 } 91 92 // check if disabled 93 if (tm.isThreadCpuTimeEnabled()) { 94 throw new RuntimeException("TEST FAILED: " + 95 "Expected thread CPU time monitoring to be disabled"); 96 } 97 98 tm.setThreadCpuTimeEnabled(false); 99 tm.setThreadCpuTimeEnabled(false); 100 101 if (tm.isThreadCpuTimeEnabled()) { 102 throw new RuntimeException("TEST FAILED: " + 103 "Expected thread CPU time monitoring to be disabled"); 104 } 105 106 tm.setThreadCpuTimeEnabled(true); 107 if (!tm.isThreadCpuTimeEnabled()) { 108 throw new RuntimeException("TEST FAILED: " + 109 "Expected thread CPU time monitoring to be disabled"); 110 } 111 } 112 113 }