1 /*
   2  * Copyright (c) 1998, 1999, 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 4146462
  27  * @summary Priority inversion prevents call to the genSeed method from
  28  *      returning
  29  *
  30  * if the test returns, then it passed.
  31  * if the test never returns (hangs forever), then it failed.
  32  */
  33 
  34 import java.security.SecureRandom;
  35 
  36 public class Priority_Inversion {
  37     public static void main(String args[]) {
  38         int deltaPriority = 1;
  39 
  40         if (args.length == 1) {
  41             try {
  42                 deltaPriority = Integer.parseInt(args[0]);
  43             }
  44             catch (NumberFormatException nfe) {
  45                 System.err.println
  46                         ("Sorry, \"" + args[0] + "\" is not a number");
  47                 System.exit(1);
  48             }
  49         }
  50 
  51         RandomTest rand = new RandomTest();
  52         InvertTest invert = new InvertTest(deltaPriority, rand);
  53         rand.start();
  54         invert.start();
  55     }
  56 }
  57 
  58 class RandomTest extends Thread {
  59     public synchronized void run() {
  60         System.out.println("Start priority " + getPriority());
  61         // The following should take over a second
  62         SecureRandom rand = new SecureRandom();
  63         rand.nextBytes(new byte[5]);
  64     }
  65 
  66     void invertPriority() {
  67         System.out.println("Waiting ..., priority " +
  68                 Thread.currentThread().getPriority());
  69         synchronized(this) {
  70         }
  71         System.out.println("Released Lock");
  72     }
  73 }
  74 class InvertTest extends Thread {
  75     private int delta;
  76     private RandomTest rand;
  77 
  78     InvertTest(int delta, RandomTest rand) {
  79         this.delta = delta;
  80         this.rand = rand;
  81     }
  82 
  83     public void run() {
  84         setPriority(getPriority() + delta);
  85         try {
  86             sleep(500);
  87         }
  88         catch (InterruptedException ie) { }
  89         rand.invertPriority();
  90     }
  91 }