KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Deadlock


1 /*
2  * @(#)Deadlock.java 1.5 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)Deadlock.java 1.5 05/11/17
39  */

40
41 import java.util.concurrent.CyclicBarrier JavaDoc;
42 import java.util.concurrent.BrokenBarrierException JavaDoc;
43 import java.util.concurrent.locks.Lock JavaDoc;
44 import java.util.concurrent.locks.ReentrantLock JavaDoc;
45 import java.io.IOException JavaDoc;
46
47 /**
48  * This Deadlock class demonstrates the capability of performing
49  * deadlock detection programmatically within the application using
50  * the java.lang.management API.
51  *
52  * See ThreadMonitor.java for the use of java.lang.management.ThreadMXBean
53  * API.
54  */

55 public class Deadlock {
56     public static void main(String JavaDoc[] argv) {
57         Deadlock dl = new Deadlock();
58  
59         // Now find deadlock
60
ThreadMonitor monitor = new ThreadMonitor();
61         boolean found = false;
62         while (!found) {
63             found = monitor.findDeadlock();
64             try {
65                 Thread.sleep(500);
66             } catch (InterruptedException JavaDoc e) {
67                 System.exit(1);
68             }
69         }
70
71         System.out.println("\nPress <Enter> to exit this Deadlock program.\n");
72         waitForEnterPressed();
73     }
74
75
76     private CyclicBarrier JavaDoc barrier = new CyclicBarrier JavaDoc(6);
77     public Deadlock() {
78         DeadlockThread[] dThreads = new DeadlockThread[6];
79
80         Monitor a = new Monitor("a");
81         Monitor b = new Monitor("b");
82         Monitor c = new Monitor("c");
83         dThreads[0] = new DeadlockThread("MThread-1", a, b);
84         dThreads[1] = new DeadlockThread("MThread-2", b, c);
85         dThreads[2] = new DeadlockThread("MThread-3", c, a);
86
87         Lock JavaDoc d = new ReentrantLock JavaDoc();
88         Lock JavaDoc e = new ReentrantLock JavaDoc();
89         Lock JavaDoc f = new ReentrantLock JavaDoc();
90
91         dThreads[3] = new DeadlockThread("SThread-4", d, e);
92         dThreads[4] = new DeadlockThread("SThread-5", e, f);
93         dThreads[5] = new DeadlockThread("SThread-6", f, d);
94                                                                                 
95         // make them daemon threads so that the test will exit
96
for (int i = 0; i < 6; i++) {
97             dThreads[i].setDaemon(true);
98             dThreads[i].start();
99         }
100     }
101
102     class DeadlockThread extends Thread JavaDoc {
103         private Lock JavaDoc lock1 = null;
104         private Lock JavaDoc lock2 = null;
105         private Monitor mon1 = null;
106         private Monitor mon2 = null;
107         private boolean useSync;
108
109         DeadlockThread(String JavaDoc name, Lock JavaDoc lock1, Lock JavaDoc lock2) {
110             super(name);
111             this.lock1 = lock1;
112             this.lock2 = lock2;
113             this.useSync = true;
114         }
115         DeadlockThread(String JavaDoc name, Monitor mon1, Monitor mon2) {
116             super(name);
117             this.mon1 = mon1;
118             this.mon2 = mon2;
119             this.useSync = false;
120         }
121         public void run() {
122             if (useSync) {
123                 syncLock();
124             } else {
125                 monitorLock();
126             }
127         }
128         private void syncLock() {
129             lock1.lock();
130             try {
131                 try {
132                     barrier.await();
133                 } catch (InterruptedException JavaDoc e) {
134                     e.printStackTrace();
135                     System.exit(1);
136                 } catch (BrokenBarrierException JavaDoc e) {
137                     e.printStackTrace();
138                     System.exit(1);
139                 }
140                 goSyncDeadlock();
141             } finally {
142                 lock1.unlock();
143             }
144         }
145         private void goSyncDeadlock() {
146             try {
147                 barrier.await();
148             } catch (InterruptedException JavaDoc e) {
149                 e.printStackTrace();
150                 System.exit(1);
151             } catch (BrokenBarrierException JavaDoc e) {
152                 e.printStackTrace();
153                 System.exit(1);
154             }
155             lock2.lock();
156             throw new RuntimeException JavaDoc("should not reach here.");
157         }
158         private void monitorLock() {
159             synchronized (mon1) {
160                 try {
161                     barrier.await();
162                 } catch (InterruptedException JavaDoc e) {
163                     e.printStackTrace();
164                     System.exit(1);
165                 } catch (BrokenBarrierException JavaDoc e) {
166                     e.printStackTrace();
167                     System.exit(1);
168                 }
169                 goMonitorDeadlock();
170             }
171         }
172         private void goMonitorDeadlock() {
173             try {
174                 barrier.await();
175             } catch (InterruptedException JavaDoc e) {
176                 e.printStackTrace();
177                 System.exit(1);
178             } catch (BrokenBarrierException JavaDoc e) {
179                 e.printStackTrace();
180                 System.exit(1);
181             }
182             synchronized (mon2) {
183                 throw new RuntimeException JavaDoc(getName() + " should not reach here.");
184             }
185         }
186     }
187
188     class Monitor {
189         String JavaDoc name;
190         Monitor(String JavaDoc name) {
191             this.name = name;
192         }
193     }
194
195     private static void waitForEnterPressed() {
196         try {
197             boolean done = false;
198             while (!done) {
199                 char ch = (char) System.in.read();
200                 if (ch<0||ch=='\n') {
201                     done = true;
202                 }
203             }
204         }
205         catch (IOException JavaDoc e) {
206             e.printStackTrace();
207             System.exit(0);
208         }
209     }
210 }
211
Popular Tags