author | sjiang |
Mon, 15 Dec 2014 19:21:59 +0100 | |
changeset 28061 | 2a10901eac1b |
parent 5506 | 202f599c92aa |
child 30376 | 2ccf2cf7ea48 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
28061
2a10901eac1b
8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 6331746 |
|
28061
2a10901eac1b
8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents:
5506
diff
changeset
|
27 |
* @summary Test a deadlock and will be blocked forever if the deadlock is present. |
2 | 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 |
synchronized(bb) { |
|
28061
2a10901eac1b
8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents:
5506
diff
changeset
|
59 |
while(!bb.gotLock) { |
2a10901eac1b
8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents:
5506
diff
changeset
|
60 |
bb.wait(); // if blocked here, means failing to get lock, impossible. |
2 | 61 |
} |
62 |
} |
|
63 |
||
64 |
System.out.println("main: The BadBay is holding the lock forever."); |
|
65 |
||
66 |
System.out.println("main: Create a WorkingBoy to see blocking ..."); |
|
67 |
WorkingBoy wb = new WorkingBoy(dt); |
|
68 |
||
69 |
synchronized(wb) { |
|
70 |
wb.start(); |
|
71 |
||
28061
2a10901eac1b
8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents:
5506
diff
changeset
|
72 |
while(!wb.done) { |
2a10901eac1b
8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents:
5506
diff
changeset
|
73 |
wb.wait(); // if blocked here, the deadlock happends |
2 | 74 |
} |
75 |
} |
|
76 |
||
77 |
System.out.println("main: OK, bye bye."); |
|
78 |
} |
|
79 |
||
80 |
private static class BadBoy extends Thread { |
|
81 |
public BadBoy(Object o) { |
|
82 |
setDaemon(true); |
|
83 |
||
84 |
this.o = o; |
|
85 |
} |
|
86 |
||
87 |
public void run() { |
|
88 |
System.out.println("BadBoy-run: keep synchronization lock forever!"); |
|
89 |
||
90 |
synchronized(o) { |
|
91 |
synchronized(this) { |
|
92 |
gotLock = true; |
|
93 |
||
94 |
this.notify(); |
|
95 |
} |
|
96 |
||
97 |
try { |
|
98 |
Thread.sleep(10000000); |
|
99 |
} catch (Exception e) { |
|
100 |
// OK |
|
101 |
} |
|
102 |
} |
|
103 |
} |
|
104 |
||
105 |
final Object o; |
|
106 |
public boolean gotLock; |
|
107 |
} |
|
108 |
||
109 |
private static class WorkingBoy extends Thread { |
|
110 |
public WorkingBoy(DeadlockTest sm) { |
|
111 |
setDaemon(true); |
|
112 |
||
113 |
this.sm = sm; |
|
114 |
} |
|
115 |
||
116 |
public void run() { |
|
117 |
try { |
|
118 |
System.out.println("WorkingBoy-run: calling StandardMBean methods ..."); |
|
119 |
||
120 |
System.out.println("WorkingBoy-run: calling setImplementation ..."); |
|
121 |
sm.setImplementation(new Timer()); |
|
122 |
||
123 |
System.out.println("WorkingBoy-run: calling getImplementation ..."); |
|
124 |
sm.getImplementation(); |
|
125 |
||
126 |
System.out.println("WorkingBoy-run: calling getMBeanInterface ..."); |
|
127 |
sm.getMBeanInterface(); |
|
128 |
||
129 |
System.out.println("WorkingBoy-run: calling getImplementationClass ..."); |
|
130 |
sm.getImplementationClass(); |
|
131 |
||
132 |
System.out.println("WorkingBoy-run: calling cacheMBeanInfo ..."); |
|
133 |
sm.cacheMBeanInfo(null); |
|
134 |
||
135 |
System.out.println("WorkingBoy-run: calling getCachedMBeanInfo ..."); |
|
136 |
sm.getCachedMBeanInfo(); |
|
137 |
||
138 |
System.out.println("WorkingBoy-run: All done!"); |
|
139 |
||
140 |
synchronized(this) { |
|
141 |
done = true; |
|
142 |
||
143 |
this.notifyAll(); |
|
144 |
} |
|
145 |
} catch (NotCompliantMBeanException ne) { |
|
146 |
// Impossible? |
|
147 |
throw new RuntimeException(ne); |
|
148 |
} |
|
149 |
} |
|
150 |
||
151 |
final DeadlockTest sm; |
|
152 |
public boolean done; |
|
153 |
} |
|
154 |
} |