blob: 2a1441cf8cb4234aa0ceda1f0b67ec841ff521ab [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 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 * @summary SynchronizerDeadlock creates threads that are deadlocked
26 * waiting for JSR-166 synchronizers.
27 * @author Mandy Chung
28 * @build Barrier
29 */
30
31import java.lang.management.*;
32import java.util.*;
33import java.util.concurrent.locks.*;
34
35public class SynchronizerDeadlock {
36
37 private Lock a = new ReentrantLock();
38 private Lock b = new ReentrantLock();
39 private Lock c = new ReentrantLock();
40 private final int EXPECTED_THREADS = 3;
41 private Thread[] dThreads = new Thread[EXPECTED_THREADS];
42 private Barrier go = new Barrier(1);
43 private Barrier barr = new Barrier(EXPECTED_THREADS);
44
45 public SynchronizerDeadlock() {
46 dThreads[0] = new DeadlockingThread("Deadlock-Thread-1", a, b);
47 dThreads[1] = new DeadlockingThread("Deadlock-Thread-2", b, c);
48 dThreads[2] = new DeadlockingThread("Deadlock-Thread-3", c, a);
49
50 // make them daemon threads so that the test will exit
51 for (int i = 0; i < EXPECTED_THREADS; i++) {
52 dThreads[i].setDaemon(true);
53 dThreads[i].start();
54 }
55 }
56
57 void goDeadlock() {
58 // Wait until all threads have started
59 barr.await();
60
61 // reset for later signals
62 barr.set(EXPECTED_THREADS);
63
64 while (go.getWaiterCount() != EXPECTED_THREADS) {
65 synchronized(this) {
66 try {
67 wait(100);
68 } catch (InterruptedException e) {
69 // ignore
70 }
71 }
72 }
73
74 // sleep a little so that all threads are blocked before notified.
75 try {
76 Thread.sleep(100);
77 } catch (InterruptedException e) {
78 // ignore
79 }
80 go.signal();
81
82 }
83
84 void waitUntilDeadlock() {
85 barr.await();
86 // sleep a little while to wait until threads are blocked.
87 try {
88 Thread.sleep(100);
89 } catch (InterruptedException e) {
90 // ignore
91 }
92 }
93
94 private class DeadlockingThread extends Thread {
95 private final Lock lock1;
96 private final Lock lock2;
97
98 DeadlockingThread(String name, Lock lock1, Lock lock2) {
99 super(name);
100 this.lock1 = lock1;
101 this.lock2 = lock2;
102 }
103 public void run() {
104 f();
105 }
106 private void f() {
107 lock1.lock();
108 try {
109 barr.signal();
110 go.await();
111 g();
112 } finally {
113 lock1.unlock();
114 }
115 }
116 private void g() {
117 barr.signal();
118 lock2.lock();
119 throw new RuntimeException("should not reach here.");
120 }
121 }
122
123 void checkResult(long[] threads) {
124 if (threads.length != EXPECTED_THREADS) {
125 ThreadDump.threadDump();
126 throw new RuntimeException("Expected to have " +
127 EXPECTED_THREADS + " to be in the deadlock list");
128 }
129 boolean[] found = new boolean[EXPECTED_THREADS];
130 for (int i = 0; i < threads.length; i++) {
131 for (int j = 0; j < dThreads.length; j++) {
132 if (dThreads[j].getId() == threads[i]) {
133 found[j] = true;
134 }
135 }
136 }
137 boolean ok = true;
138 for (int j = 0; j < found.length; j++) {
139 ok = ok && found[j];
140 }
141
142 if (!ok) {
143 System.out.print("Returned result is [");
144 for (int j = 0; j < threads.length; j++) {
145 System.out.print(threads[j] + " ");
146 }
147 System.out.println("]");
148
149 System.out.print("Expected result is [");
150 for (int j = 0; j < threads.length; j++) {
151 System.out.print(dThreads[j] + " ");
152 }
153 System.out.println("]");
154 throw new RuntimeException("Unexpected result returned " +
155 " by findMonitorDeadlockedThreads method.");
156 }
157 }
158}