1 /* 2 * Copyright (c) 2012, 2018, 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 * @comment Use othervm mode so that we don't capture unrelated threads created by other tests 34 * @run main/othervm ThreadPriorities 35 */ 36 37 import java.util.ArrayList; 38 import java.util.concurrent.CyclicBarrier; 39 import java.util.regex.Matcher; 40 import java.util.regex.Pattern; 41 42 import jdk.test.lib.process.ProcessTools; 43 import jdk.test.lib.process.OutputAnalyzer; 44 import jdk.test.lib.JDKToolFinder; 45 import static jdk.test.lib.Asserts.*; 46 47 public class ThreadPriorities { 48 49 public static void main(String[] args) throws Throwable { 50 final int NUMBER_OF_JAVA_PRIORITIES = 51 Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1; 52 final CyclicBarrier barrier = 53 new CyclicBarrier(NUMBER_OF_JAVA_PRIORITIES + 1); 54 55 for (int p = Thread.MIN_PRIORITY; p <= Thread.MAX_PRIORITY; ++p) { 56 final int priority = p; 57 new Thread("Priority=" + p) { 58 { 59 setPriority(priority); 60 } 61 public void run() { 62 try { 63 barrier.await(); // 1st 64 barrier.await(); // 2nd 65 } catch (Exception exc) { 66 // ignore 67 } 68 } 69 }.start(); 70 } 71 barrier.await(); // 1st 72 73 int matches = 0; 74 ArrayList<String> failed = new ArrayList<>(); 75 ProcessBuilder pb = new ProcessBuilder( 76 JDKToolFinder.getJDKTool("jstack"), 77 String.valueOf(ProcessTools.getProcessId())); 78 79 String[] output = new OutputAnalyzer(pb.start()).getOutput().split("\\R"); 80 81 Pattern pattern = Pattern.compile( 82 "\\\"Priority=(\\d+)\\\".* prio=(\\d+).*"); 83 for (String line : output) { 84 Matcher matcher = pattern.matcher(line); 85 if (matcher.matches()) { 86 matches += 1; 87 String expected = matcher.group(1); 88 String actual = matcher.group(2); 89 if (!expected.equals(actual)) { 90 failed.add(line); 91 } 92 } 93 } 94 barrier.await(); // 2nd 95 barrier.reset(); 96 97 boolean success = false; 98 try { 99 assertEquals(matches, NUMBER_OF_JAVA_PRIORITIES); 100 assertTrue(failed.isEmpty(), failed.size() + ":" + failed); 101 success = true; 102 } 103 finally { 104 if (!success) { 105 System.out.println("Failure detected - dumping jstack output:"); 106 for (String line : output) { 107 System.out.println(line); 108 } 109 } 110 } 111 } 112 } 113