1 /*
   2  * Copyright (c) 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 import java.lang.management.*;
  24 
  25 /*
  26  * @test
  27  * @bug     8205878
  28  * @requires os.family != "windows"
  29  * @summary Basic test of Thread and ThreadMXBean queries on a natively
  30  *          attached thread that has failed to detach before terminating.
  31  * @run main/native TestTerminatedThread
  32  */
  33 
  34 public class TestTerminatedThread {
  35 
  36     static native Thread createTerminatedThread();
  37 
  38     static {
  39         System.loadLibrary("terminatedThread");
  40     }
  41 
  42     private static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
  43 
  44     public static void main(String[] args) throws Throwable {
  45 
  46         Thread t = createTerminatedThread();
  47 
  48         if (!t.isAlive())
  49             throw new Error("Thread is only supposed to terminate at native layer!");
  50 
  51         // Now invoke the various functions on this thread to
  52         // make sure the VM handles it okay. The focus is on
  53         // functions with an underlying native OS implementation.
  54         // Generally as long as we don't crash or throw unexpected
  55         // exceptions then the test passes. In some cases we know exactly
  56         // what a function should return and so can check that.
  57 
  58         System.out.println("Working with thread: " + t +
  59                            ",  in state: " + t.getState());
  60 
  61         System.out.println("Calling suspend ...");
  62         t.suspend();
  63         System.out.println("Calling resume ...");
  64         t.resume();
  65         System.out.println("Calling getStackTrace ...");
  66         StackTraceElement[] stack = t.getStackTrace();
  67         System.out.println(java.util.Arrays.toString(stack));
  68         if (stack.length != 0)
  69             throw new Error("Terminated thread should have empty java stack trace");
  70         System.out.println("Calling setName(\"NewName\") ...");
  71         t.setName("NewName");
  72         System.out.println("Calling interrupt ...");
  73         t.interrupt();
  74         System.out.println("Calling stop ...");
  75         t.stop();
  76 
  77         // Now the ThreadMXBean functions
  78 
  79         if (mbean.isThreadCpuTimeSupported() &&
  80             mbean.isThreadCpuTimeEnabled() ) {
  81             System.out.println("Calling getThreadCpuTime ...");
  82             long t1 = mbean.getThreadCpuTime(t.getId());
  83             if (t1 != -1) {
  84                 throw new RuntimeException("Invalid ThreadCpuTime returned = " +
  85                                            t1 + " expected = -1");
  86             }
  87             System.out.println("Okay: getThreadCpuTime() reported -1 as expected");
  88         } else {
  89             System.out.println("Skipping Thread CPU time test as it's not supported");
  90         }
  91 
  92         System.out.println("Calling getThreadUserTime ...");
  93         long t1 = mbean.getThreadUserTime(t.getId());
  94         if (t1 != -1) {
  95             throw new RuntimeException("Invalid ThreadUserTime returned = " +
  96                                        t1 + " expected = -1");
  97         }
  98         System.out.println("Okay: getThreadUserTime() reported -1 as expected");
  99 
 100         System.out.println("Calling getThreadInfo ...");
 101         ThreadInfo info = mbean.getThreadInfo(t.getId());
 102         System.out.println(info);
 103 
 104         System.out.println("Calling getThreadInfo with stack ...");
 105         info = mbean.getThreadInfo(t.getId(), Integer.MAX_VALUE);
 106         System.out.println(info);
 107     }
 108 }