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 issuspended002 { 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 int count = 0; 79 while (true) { 80 info = mbean.getThreadInfo(id, Integer.MAX_VALUE); 81 if (info == null) { 82 // the thread has exited 83 break; 84 } 85 count++; 86 isSuspended = info.isSuspended(); 87 if (isSuspended) { 88 out.println("Failure 4."); 89 out.println("ThreadInfo.isSuspended() returned true, after " 90 + "thread.die was set to true."); 91 testFailed = true; 92 break; 93 } 94 } 95 96 out.println("INFO: made " + count + " late getThreadInfo() calls."); 97 98 if (testFailed) 99 out.println("TEST FAILED"); 100 return (testFailed) ? Consts.TEST_FAILED : Consts.TEST_PASSED; 101 } 102 103 private static class MyThread extends Thread { 104 final static long WAIT_TIME = 500; // Milliseconds 105 Object object = new Object(); 106 volatile boolean die = false; 107 PrintStream out; 108 MyThread(PrintStream out)109 MyThread(PrintStream out) { 110 this.out = out; 111 } 112 run()113 public void run() { 114 115 // Notify "main" thread that MyThread has started 116 mainEntrance.unlock(); 117 118 while (!die) { 119 synchronized(object) { 120 try { 121 object.wait(WAIT_TIME); 122 } catch (InterruptedException e) { 123 out.println("Unexpected exception."); 124 e.printStackTrace(out); 125 testFailed = true; 126 } 127 } // synchronized 128 } 129 } // run() 130 } // MyThread 131 } 132