2
|
1 |
/*
|
|
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 |
* @test
|
|
26 |
* @bug 6331746
|
|
27 |
* @summary Deadlock on synchronization problem
|
|
28 |
* @author Shanliang JIANG
|
|
29 |
* @run main DeadlockTest
|
|
30 |
*/
|
|
31 |
|
|
32 |
import javax.management.*;
|
|
33 |
import javax.management.timer.*;
|
|
34 |
|
|
35 |
public class DeadlockTest extends StandardMBean {
|
|
36 |
public <T> DeadlockTest(T implementation, Class<T> mbeanInterface)
|
|
37 |
throws NotCompliantMBeanException {
|
|
38 |
super(implementation, mbeanInterface);
|
|
39 |
}
|
|
40 |
|
|
41 |
public MBeanInfo getCachedMBeanInfo() {
|
|
42 |
return super.getCachedMBeanInfo();
|
|
43 |
}
|
|
44 |
|
|
45 |
public void cacheMBeanInfo(MBeanInfo mi) {
|
|
46 |
super.cacheMBeanInfo(mi);
|
|
47 |
}
|
|
48 |
|
|
49 |
public static void main(String[] args) throws Exception {
|
|
50 |
System.out.println("main: No deadlock please.");
|
|
51 |
|
|
52 |
System.out.println("main: Create a BadBay to hold the lock forever.");
|
|
53 |
DeadlockTest dt = new DeadlockTest(new Timer(), TimerMBean.class);
|
|
54 |
|
|
55 |
BadBoy bb = new BadBoy(dt);
|
|
56 |
bb.start();
|
|
57 |
|
|
58 |
final long timeout = 2000;
|
|
59 |
long stopTime = System.currentTimeMillis() + timeout;
|
|
60 |
long timeToWait = timeout;
|
|
61 |
synchronized(bb) {
|
|
62 |
while(!bb.gotLock || timeToWait > 0) {
|
|
63 |
bb.wait(timeToWait);
|
|
64 |
|
|
65 |
timeToWait = stopTime - System.currentTimeMillis();
|
|
66 |
}
|
|
67 |
}
|
|
68 |
|
|
69 |
if (!bb.gotLock) {
|
|
70 |
throw new RuntimeException("Failed to get lock, impossible!");
|
|
71 |
}
|
|
72 |
|
|
73 |
System.out.println("main: The BadBay is holding the lock forever.");
|
|
74 |
|
|
75 |
System.out.println("main: Create a WorkingBoy to see blocking ...");
|
|
76 |
WorkingBoy wb = new WorkingBoy(dt);
|
|
77 |
|
|
78 |
stopTime = System.currentTimeMillis() + timeout;
|
|
79 |
timeToWait = timeout;
|
|
80 |
|
|
81 |
synchronized(wb) {
|
|
82 |
wb.start();
|
|
83 |
|
|
84 |
while(!wb.done || timeToWait > 0) {
|
|
85 |
wb.wait(timeToWait);
|
|
86 |
|
|
87 |
timeToWait = stopTime - System.currentTimeMillis();
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
if (!wb.done) {
|
|
92 |
throw new RuntimeException("It is blocked!");
|
|
93 |
}
|
|
94 |
|
|
95 |
System.out.println("main: OK, bye bye.");
|
|
96 |
}
|
|
97 |
|
|
98 |
private static class BadBoy extends Thread {
|
|
99 |
public BadBoy(Object o) {
|
|
100 |
setDaemon(true);
|
|
101 |
|
|
102 |
this.o = o;
|
|
103 |
}
|
|
104 |
|
|
105 |
public void run() {
|
|
106 |
System.out.println("BadBoy-run: keep synchronization lock forever!");
|
|
107 |
|
|
108 |
synchronized(o) {
|
|
109 |
synchronized(this) {
|
|
110 |
gotLock = true;
|
|
111 |
|
|
112 |
this.notify();
|
|
113 |
}
|
|
114 |
|
|
115 |
try {
|
|
116 |
Thread.sleep(10000000);
|
|
117 |
} catch (Exception e) {
|
|
118 |
// OK
|
|
119 |
}
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
final Object o;
|
|
124 |
public boolean gotLock;
|
|
125 |
}
|
|
126 |
|
|
127 |
private static class WorkingBoy extends Thread {
|
|
128 |
public WorkingBoy(DeadlockTest sm) {
|
|
129 |
setDaemon(true);
|
|
130 |
|
|
131 |
this.sm = sm;
|
|
132 |
}
|
|
133 |
|
|
134 |
public void run() {
|
|
135 |
try {
|
|
136 |
System.out.println("WorkingBoy-run: calling StandardMBean methods ...");
|
|
137 |
|
|
138 |
System.out.println("WorkingBoy-run: calling setImplementation ...");
|
|
139 |
sm.setImplementation(new Timer());
|
|
140 |
|
|
141 |
System.out.println("WorkingBoy-run: calling getImplementation ...");
|
|
142 |
sm.getImplementation();
|
|
143 |
|
|
144 |
System.out.println("WorkingBoy-run: calling getMBeanInterface ...");
|
|
145 |
sm.getMBeanInterface();
|
|
146 |
|
|
147 |
System.out.println("WorkingBoy-run: calling getImplementationClass ...");
|
|
148 |
sm.getImplementationClass();
|
|
149 |
|
|
150 |
System.out.println("WorkingBoy-run: calling cacheMBeanInfo ...");
|
|
151 |
sm.cacheMBeanInfo(null);
|
|
152 |
|
|
153 |
System.out.println("WorkingBoy-run: calling getCachedMBeanInfo ...");
|
|
154 |
sm.getCachedMBeanInfo();
|
|
155 |
|
|
156 |
System.out.println("WorkingBoy-run: All done!");
|
|
157 |
|
|
158 |
synchronized(this) {
|
|
159 |
done = true;
|
|
160 |
|
|
161 |
this.notifyAll();
|
|
162 |
}
|
|
163 |
} catch (NotCompliantMBeanException ne) {
|
|
164 |
// Impossible?
|
|
165 |
throw new RuntimeException(ne);
|
|
166 |
}
|
|
167 |
}
|
|
168 |
|
|
169 |
final DeadlockTest sm;
|
|
170 |
public boolean done;
|
|
171 |
}
|
|
172 |
}
|