Thu, 16 May 2013 12:14:19 -0700
Added tag jdk8-b90 for changeset 1ae0472ff3a0
1 /*
2 * Copyright (c) 2012, 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 */
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 * @run main Test7194254
31 */
33 import java.io.BufferedReader;
34 import java.io.InputStreamReader;
35 import java.lang.management.ManagementFactory;
36 import java.lang.management.RuntimeMXBean;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.concurrent.CyclicBarrier;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
43 public class Test7194254 {
45 public static void main(String[] args) throws Exception {
46 final int NUMBER_OF_JAVA_PRIORITIES =
47 Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1;
48 final CyclicBarrier barrier =
49 new CyclicBarrier(NUMBER_OF_JAVA_PRIORITIES + 1);
51 for (int p = Thread.MIN_PRIORITY; p <= Thread.MAX_PRIORITY; ++p) {
52 final int priority = p;
53 new Thread("Priority=" + p) {
54 {
55 setPriority(priority);
56 }
57 public void run() {
58 try {
59 barrier.await(); // 1st
60 barrier.await(); // 2nd
61 } catch (Exception exc) {
62 // ignore
63 }
64 }
65 }.start();
66 }
67 barrier.await(); // 1st
69 int matches = 0;
70 List<String> failed = new ArrayList<>();
71 try {
72 String pid = getPid();
73 String jstack = System.getProperty("java.home") + "/../bin/jstack";
74 Process process = new ProcessBuilder(jstack, pid)
75 .redirectErrorStream(true).start();
76 Pattern pattern = Pattern.compile(
77 "\\\"Priority=(\\d+)\\\".* prio=(\\d+).*");
78 try (BufferedReader reader = new BufferedReader(
79 new InputStreamReader(process.getInputStream()))) {
80 String line;
81 while((line = reader.readLine()) != null) {
82 Matcher matcher = pattern.matcher(line);
83 if (matcher.matches()) {
84 matches += 1;
85 String expected = matcher.group(1);
86 String actual = matcher.group(2);
87 if (!expected.equals(actual)) {
88 failed.add(line);
89 }
90 }
91 }
92 }
93 barrier.await(); // 2nd
94 } finally {
95 barrier.reset();
96 }
98 if (matches != NUMBER_OF_JAVA_PRIORITIES) {
99 throw new AssertionError("matches: expected " +
100 NUMBER_OF_JAVA_PRIORITIES + ", but was " + matches);
101 }
102 if (!failed.isEmpty()) {
103 throw new AssertionError(failed.size() + ":" + failed);
104 }
105 System.out.println("Test passes.");
106 }
108 static String getPid() {
109 RuntimeMXBean runtimebean = ManagementFactory.getRuntimeMXBean();
110 String vmname = runtimebean.getName();
111 int i = vmname.indexOf('@');
112 if (i != -1) {
113 vmname = vmname.substring(0, i);
114 }
115 return vmname;
116 }
118 }