Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/7194254/Test7194254.java
32284 views
/*1* Copyright (c) 2012, 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* @bug 719425426* @summary Creates several threads with different java priorities and checks27* whether jstack reports correct priorities for them.28*29* @run main Test719425430*/3132import java.io.BufferedReader;33import java.io.InputStreamReader;34import java.lang.management.ManagementFactory;35import java.lang.management.RuntimeMXBean;36import java.util.ArrayList;37import java.util.List;38import java.util.concurrent.CyclicBarrier;39import java.util.regex.Matcher;40import java.util.regex.Pattern;4142public class Test7194254 {4344public static void main(String[] args) throws Exception {45final int NUMBER_OF_JAVA_PRIORITIES =46Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1;47final CyclicBarrier barrier =48new CyclicBarrier(NUMBER_OF_JAVA_PRIORITIES + 1);4950for (int p = Thread.MIN_PRIORITY; p <= Thread.MAX_PRIORITY; ++p) {51final int priority = p;52new Thread("Priority=" + p) {53{54setPriority(priority);55}56public void run() {57try {58barrier.await(); // 1st59barrier.await(); // 2nd60} catch (Exception exc) {61// ignore62}63}64}.start();65}66barrier.await(); // 1st6768int matches = 0;69List<String> failed = new ArrayList<>();70try {71String pid = getPid();72String jstack = System.getProperty("java.home") + "/../bin/jstack";73Process process = new ProcessBuilder(jstack, pid)74.redirectErrorStream(true).start();75Pattern pattern = Pattern.compile(76"\\\"Priority=(\\d+)\\\".* prio=(\\d+).*");77try (BufferedReader reader = new BufferedReader(78new InputStreamReader(process.getInputStream()))) {79String line;80while((line = reader.readLine()) != null) {81Matcher matcher = pattern.matcher(line);82if (matcher.matches()) {83matches += 1;84String expected = matcher.group(1);85String actual = matcher.group(2);86if (!expected.equals(actual)) {87failed.add(line);88}89}90}91}92barrier.await(); // 2nd93} finally {94barrier.reset();95}9697if (matches != NUMBER_OF_JAVA_PRIORITIES) {98throw new AssertionError("matches: expected " +99NUMBER_OF_JAVA_PRIORITIES + ", but was " + matches);100}101if (!failed.isEmpty()) {102throw new AssertionError(failed.size() + ":" + failed);103}104System.out.println("Test passes.");105}106107static String getPid() {108RuntimeMXBean runtimebean = ManagementFactory.getRuntimeMXBean();109String vmname = runtimebean.getName();110int i = vmname.indexOf('@');111if (i != -1) {112vmname = vmname.substring(0, i);113}114return vmname;115}116117}118119120121