blob: 51c3957b1ab4e8024baef76d5d49f870877f955d [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-1999 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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
34import java.security.SecureRandom;
35
36public 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
58class 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}
74class 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}