Path: blob/master/test/hotspot/jtreg/serviceability/tmtools/jstack/DaemonThreadTest.java
41153 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Create daemon and non-deamon threads.26* Check the correctness of thread's status from jstack.27* @modules java.base/jdk.internal.misc28* @library /test/lib29* @library ../share30* @run main/othervm -XX:+UsePerfData DaemonThreadTest31*/32import common.ToolResults;33import utils.*;3435public class DaemonThreadTest {3637static class NormalThread extends Thread {3839NormalThread() {40}4142@Override43public void run() {44Utils.sleep();45}4647}4849static class DaemonThread extends Thread {5051DaemonThread() {52setDaemon(true);53}5455@Override56public void run() {57Utils.sleep();58}5960}6162public static void main(String[] args) throws Exception {63testNoDaemon();64testDaemon();65}6667private static void testNoDaemon() throws Exception {68testThread(new NormalThread(), "");69}7071private static void testDaemon() throws Exception {72testThread(new DaemonThread(), "daemon");73}7475private static void testThread(Thread thread, String expectedType) throws Exception {76// Start the thread77thread.start();7879// Run jstack tool and collect the output80JstackTool jstackTool = new JstackTool(ProcessHandle.current().pid());81ToolResults results = jstackTool.measure();8283// Analyze the jstack output for the correct thread type84JStack jstack = new DefaultFormat().parse(results.getStdoutString());85ThreadStack ti = jstack.getThreadStack(thread.getName());8687if (!ti.getType().trim().equals(expectedType)) {88throw new RuntimeException("incorrect thread type '" + ti.getType() + "' for the thread '" + thread.getName() + "'");89}9091}9293}949596