1 /* 2 * Copyright (c) 2004, 2018, 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 package nsk.monitoring.ThreadInfo.isSuspended; 25 26 import java.lang.management.*; 27 import java.io.*; 28 import nsk.share.*; 29 30 public class issuspended001 { 31 private static Wicket mainEntrance = new Wicket(); 32 private static boolean testFailed = false; 33 main(String[] argv)34 public static void main(String[] argv) { 35 System.exit(Consts.JCK_STATUS_BASE + run(argv, System.out)); 36 } 37 run(String[] argv, PrintStream out)38 public static int run(String[] argv, PrintStream out) { 39 ThreadMXBean mbean = ManagementFactory.getThreadMXBean(); 40 MyThread thread = new MyThread(out); 41 thread.start(); 42 43 // Wait for MyThread to start 44 mainEntrance.waitFor(); 45 46 long id = thread.getId(); 47 ThreadInfo info = mbean.getThreadInfo(id, Integer.MAX_VALUE); 48 boolean isSuspended = info.isSuspended(); 49 if (isSuspended) { 50 out.println("Failure 1."); 51 out.println("ThreadInfo.isSuspended() returned true, before " 52 + "Thread.suspend() was invoked."); 53 testFailed = true; 54 } 55 56 thread.suspend(); 57 info = mbean.getThreadInfo(id, Integer.MAX_VALUE); 58 isSuspended = info.isSuspended(); 59 if (!isSuspended) { 60 out.println("Failure 2."); 61 out.println("ThreadInfo.isSuspended() returned false, after " 62 + "Thread.suspend() was invoked."); 63 testFailed = true; 64 } 65 66 thread.resume(); 67 info = mbean.getThreadInfo(id, Integer.MAX_VALUE); 68 isSuspended = info.isSuspended(); 69 if (isSuspended) { 70 out.println("Failure 3."); 71 out.println("ThreadInfo.isSuspended() returned true, after " 72 + "Thread.resume() was invoked."); 73 testFailed = true; 74 } 75 76 thread.die = true; 77 78 if (testFailed) 79 out.println("TEST FAILED"); 80 return (testFailed) ? Consts.TEST_FAILED : Consts.TEST_PASSED; 81 } 82 83 private static class MyThread extends Thread { 84 final static long WAIT_TIME = 500; // Milliseconds 85 Object object = new Object(); 86 boolean die = false; 87 PrintStream out; 88 MyThread(PrintStream out)89 MyThread(PrintStream out) { 90 this.out = out; 91 } 92 run()93 public void run() { 94 95 // Notify "main" thread that MyThread has started 96 mainEntrance.unlock(); 97 98 while (!die) { 99 synchronized(object) { 100 try { 101 object.wait(WAIT_TIME); 102 } catch (InterruptedException e) { 103 out.println("Unexpected exception."); 104 e.printStackTrace(out); 105 testFailed = true; 106 } 107 } // synchronized 108 } 109 } // run() 110 } // MyThread 111 } 112