1 /* 2 * Copyright (c) 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 * @summary Create daemon and non-deamon threads. 27 * Check the correctness of thread's status from jstack. 28 * @modules java.base/jdk.internal.misc 29 * @library /test/lib/share/classes 30 * @library ../share 31 * @build common.* 32 * 33 * @run main/othervm -XX:+UsePerfData DaemonThreadTest 34 */ 35 import common.ToolResults; 36 import utils.*; 37 38 public class DaemonThreadTest { 39 40 static class NormalThread extends Thread { 41 42 NormalThread() { 43 } 44 45 @Override 46 public void run() { 47 Utils.sleep(); 48 } 49 50 } 51 52 static class DaemonThread extends Thread { 53 54 DaemonThread() { 55 setDaemon(true); 56 } 57 58 @Override 59 public void run() { 60 Utils.sleep(); 61 } 62 63 } 64 65 public static void main(String[] args) throws Exception { 66 testNoDaemon(); 67 testDaemon(); 68 } 69 70 private static void testNoDaemon() throws Exception { 71 testThread(new NormalThread(), ""); 72 } 73 74 private static void testDaemon() throws Exception { 75 testThread(new DaemonThread(), "daemon"); 76 } 77 78 private static void testThread(Thread thread, String expectedType) throws Exception { 79 // Start the thread 80 thread.start(); 81 82 // Run jstack tool and collect the output 83 JstackTool jstackTool = new JstackTool(ProcessHandle.current().getPid()); 84 ToolResults results = jstackTool.measure(); 85 86 // Analyze the jstack output for the correct thread type 87 JStack jstack = new DefaultFormat().parse(results.getStdoutString()); 88 ThreadStack ti = jstack.getThreadStack(thread.getName()); 89 90 if (!ti.getType().trim().equals(expectedType)) { 91 throw new RuntimeException("incorrect thread type '" + ti.getType() + "' for the thread '" + thread.getName() + "'"); 92 } 93 94 } 95 96 }