1 /* 2 * Copyright (c) 2012, 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 * @bug 7194254 27 * @summary Creates several threads with different java priorities and checks 28 * whether jstack reports correct priorities for them. 29 * 30 * @library /testlibrary 31 * @modules java.base/jdk.internal.misc 32 * java.management 33 * @run main ThreadPriorities 34 */ 35 36 import java.util.ArrayList; 37 import java.util.concurrent.CyclicBarrier; 38 import java.util.regex.Matcher; 39 import java.util.regex.Pattern; 40 41 import jdk.test.lib.*; 42 import static jdk.test.lib.Asserts.*; 43 44 public class ThreadPriorities { 45 46 public static void main(String[] args) throws Throwable { 47 final int NUMBER_OF_JAVA_PRIORITIES = 48 Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1; 49 final CyclicBarrier barrier = 50 new CyclicBarrier(NUMBER_OF_JAVA_PRIORITIES + 1); 51 52 for (int p = Thread.MIN_PRIORITY; p <= Thread.MAX_PRIORITY; ++p) { 53 final int priority = p; 54 new Thread("Priority=" + p) { 55 { 56 setPriority(priority); 57 } 58 public void run() { 59 try { 60 barrier.await(); // 1st 61 barrier.await(); // 2nd 62 } catch (Exception exc) { 63 // ignore 64 } 65 } 66 }.start(); 67 } 68 barrier.await(); // 1st 69 70 int matches = 0; 71 ArrayList<String> failed = new ArrayList<>(); 72 ProcessBuilder pb = new ProcessBuilder( 73 JDKToolFinder.getJDKTool("jstack"), 74 String.valueOf(ProcessTools.getProcessId())); 75 76 String[] output = new OutputAnalyzer(pb.start()).getOutput().split("\\n+"); 77 78 Pattern pattern = Pattern.compile( 79 "\\\"Priority=(\\d+)\\\".* prio=(\\d+).*"); 80 for (String line : output) { 81 Matcher matcher = pattern.matcher(line); 82 if (matcher.matches()) { 83 matches += 1; 84 String expected = matcher.group(1); 85 String actual = matcher.group(2); 86 if (!expected.equals(actual)) { 87 failed.add(line); 88 } 89 } 90 } 91 barrier.await(); // 2nd 92 barrier.reset(); 93 94 assertEquals(matches, NUMBER_OF_JAVA_PRIORITIES); 95 assertTrue(failed.isEmpty(), failed.size() + ":" + failed); 96 } 97 } 98