1 /* 2 * Copyright (c) 2012, 2016, 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 /test/lib 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.process.ProcessTools; 42 import jdk.test.lib.process.OutputAnalyzer; 43 import jdk.test.lib.JDKToolFinder; 44 import static jdk.test.lib.Asserts.*; 45 46 public class ThreadPriorities { 47 48 public static void main(String[] args) throws Throwable { 49 final int NUMBER_OF_JAVA_PRIORITIES = 50 Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1; 51 final CyclicBarrier barrier = 52 new CyclicBarrier(NUMBER_OF_JAVA_PRIORITIES + 1); 53 54 for (int p = Thread.MIN_PRIORITY; p <= Thread.MAX_PRIORITY; ++p) { 55 final int priority = p; 56 new Thread("Priority=" + p) { 57 { 58 setPriority(priority); 59 } 60 public void run() { 61 try { 62 barrier.await(); // 1st 63 barrier.await(); // 2nd 64 } catch (Exception exc) { 65 // ignore 66 } 67 } 68 }.start(); 69 } 70 barrier.await(); // 1st 71 72 int matches = 0; 73 ArrayList<String> failed = new ArrayList<>(); 74 ProcessBuilder pb = new ProcessBuilder( 75 JDKToolFinder.getJDKTool("jstack"), 76 String.valueOf(ProcessTools.getProcessId())); 77 78 String[] output = new OutputAnalyzer(pb.start()).getOutput().split("\\n+"); 79 80 Pattern pattern = Pattern.compile( 81 "\\\"Priority=(\\d+)\\\".* prio=(\\d+).*"); 82 for (String line : output) { 83 Matcher matcher = pattern.matcher(line); 84 if (matcher.matches()) { 85 matches += 1; 86 String expected = matcher.group(1); 87 String actual = matcher.group(2); 88 if (!expected.equals(actual)) { 89 failed.add(line); 90 } 91 } 92 } 93 barrier.await(); // 2nd 94 barrier.reset(); 95 96 assertEquals(matches, NUMBER_OF_JAVA_PRIORITIES); 97 assertTrue(failed.isEmpty(), failed.size() + ":" + failed); 98 } 99 } 100