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