Path: blob/master/test/hotspot/jtreg/serviceability/tmtools/jstack/ThreadNamesTest.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 Checks that jstack correctly prints the thread names26* @modules java.base/jdk.internal.misc27* @library /test/lib28* @library ../share29* @run main/othervm -XX:+UsePerfData ThreadNamesTest30*/31import common.ToolResults;32import utils.*;3334public class ThreadNamesTest {3536private static final String STRANGE_NAME = "-_?+!@#$%^*()";37private static final String LONG_NAME = "loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong";3839static class NamedThread extends Thread {4041NamedThread(String name) {42setName(name);43}4445@Override46public void run() {47Utils.sleep();48}49}5051public static void main(String[] args) throws Exception {52testWithName(STRANGE_NAME);53testWithName("");54testWithName(LONG_NAME);55}5657private static void testWithName(String name) throws Exception {58// Start a thread with some strange name59NamedThread thread = new NamedThread(name);60thread.start();6162// Run jstack tool and collect the output63JstackTool jstackTool = new JstackTool(ProcessHandle.current().pid());64ToolResults results = jstackTool.measure();6566// Analyze the jstack output for the strange thread name67JStack jstack1 = new DefaultFormat().parse(results.getStdoutString());68ThreadStack ti1 = jstack1.getThreadStack(name);6970if (ti1 == null) {71throw new RuntimeException("jstack output doesn't contain thread info for the thread '" + name + "'");72}73}7475}767778