1 /* 2 * Copyright (c) 2003, 2010, 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 */ 31 32 import java.lang.management.ThreadMXBean; 33 import java.lang.management.ManagementFactory; 34 35 public class DisableTest { 36 private static ThreadMXBean tm = ManagementFactory.getThreadMXBean(); 37 38 public static void main(String args[]) throws Exception { 39 try { 40 testThreadContentionMonitoring(); 41 testThreadCpuMonitoring(); 42 } finally { 43 // restore the default 44 if (tm.isThreadContentionMonitoringSupported()) { 45 tm.setThreadContentionMonitoringEnabled(false); 46 } 47 if (tm.isThreadCpuTimeSupported()) { 48 tm.setThreadCpuTimeEnabled(false); 49 } 50 } 51 52 System.out.println("Test passed."); 53 } 54 55 private static void testThreadContentionMonitoring() 56 throws Exception { 57 if (!tm.isThreadContentionMonitoringSupported()) { 58 System.out.println("JVM does not supports thread contention monitoring"); 59 return; 60 } 61 62 // Default is false. 63 tm.setThreadContentionMonitoringEnabled(false); 64 tm.setThreadContentionMonitoringEnabled(false); 65 66 // check if disabled 67 if (tm.isThreadContentionMonitoringEnabled()) { 68 throw new RuntimeException("TEST FAILED: " + 69 "Expected thread contention monitoring to be disabled"); 70 } 71 72 tm.setThreadContentionMonitoringEnabled(true); 73 // check if enabled 74 if (!tm.isThreadContentionMonitoringEnabled()) { 75 throw new RuntimeException("TEST FAILED: " + 76 "Expected thread contention monitoring to be enabled"); 77 } 78 } 79 80 private static void testThreadCpuMonitoring() 81 throws Exception { 82 if (!tm.isThreadCpuTimeSupported()) { 83 System.out.println("JVM does not support thread CPU time monitoring"); 84 return; 85 } 86 87 if (tm.isThreadCpuTimeEnabled()) { 88 tm.setThreadCpuTimeEnabled(false); 89 } 90 91 // check if disabled 92 if (tm.isThreadCpuTimeEnabled()) { 93 throw new RuntimeException("TEST FAILED: " + 94 "Expected thread CPU time monitoring to be disabled"); 95 } 96 97 tm.setThreadCpuTimeEnabled(false); 98 tm.setThreadCpuTimeEnabled(false); 99 100 if (tm.isThreadCpuTimeEnabled()) { 101 throw new RuntimeException("TEST FAILED: " + 102 "Expected thread CPU time monitoring to be disabled"); 103 } 104 105 tm.setThreadCpuTimeEnabled(true); 106 if (!tm.isThreadCpuTimeEnabled()) { 107 throw new RuntimeException("TEST FAILED: " + 108 "Expected thread CPU time monitoring to be disabled"); 109 } 110 } 111 112 }