KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > mc > TestVMDeadlock


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.mc;
20
21 abstract class SyncRunnable implements Runnable JavaDoc {
22   SyncRunnable other;
23
24   public void setOther (SyncRunnable other) {
25     this.other = other;
26   }
27
28   public abstract void doSomething ();
29
30   public abstract void doSomethingElse ();
31
32   public abstract void run ();
33 }
34
35 /**
36  * deadlock detection test
37  */

38 public class TestVMDeadlock {
39   public static void main (String JavaDoc[] args) {
40     TestVMDeadlock t = new TestVMDeadlock();
41
42     if (args.length > 0) {
43       // just run the specified tests
44
for (int i = 0; i < args.length; i++) {
45         String JavaDoc func = args[i];
46
47         // note that we don't use reflection here because this would
48
// blow up execution/test scope under JPF
49
if ("testSyncMthDeadlock".equals(func)) {
50           t.testSyncMthDeadlock();
51         } else if ("testSyncBlockDeadlock".equals(func)) {
52           t.testSyncBlockDeadlock();
53         } else if ("testMixedDeadlock".equals(func)) {
54           t.testMixedDeadlock();
55         } else if ("testMissedSignalDeadlock".equals(func)) {
56           t.testMissedSignalDeadlock();
57         } else {
58           throw new IllegalArgumentException JavaDoc("unknown test function");
59         }
60       }
61     } else {
62       // quite useless to call the others, it locks up
63
t.testSyncMthDeadlock();
64     }
65   }
66
67   /**
68    * Testing for deadlocks resulting from missed signal. This is the original
69    * oldclassic example which has been turned into a test case.
70    */

71   public void testMissedSignalDeadlock () {
72     Event new_event1 = new Event();
73     Event new_event2 = new Event();
74
75     FirstTask task1 = new FirstTask(new_event1, new_event2);
76     SecondTask task2 = new SecondTask(new_event1, new_event2);
77
78     task1.start();
79     task2.start();
80   }
81
82   /**
83    * the test which mixes synchronized method attr with MONITORENTER/EXIT
84    */

85   public void testMixedDeadlock () {
86     SyncRunnable r1 = new SyncMthRunnable();
87     SyncRunnable r2 = new SyncBlockRunnable();
88     r1.setOther(r2);
89     r2.setOther(r1);
90
91     Thread JavaDoc t1 = new Thread JavaDoc(r1);
92     Thread JavaDoc t2 = new Thread JavaDoc(r2);
93
94     t1.start();
95     t2.start();
96   }
97
98   /**
99    * the test which checks MONITORENTER / MONITOREXIT
100    */

101   public void testSyncBlockDeadlock () {
102     SyncRunnable r1 = new SyncBlockRunnable();
103     SyncRunnable r2 = new SyncBlockRunnable();
104     r1.setOther(r2);
105     r2.setOther(r1);
106
107     Thread JavaDoc t1 = new Thread JavaDoc(r1);
108     Thread JavaDoc t2 = new Thread JavaDoc(r2);
109
110     t1.start();
111     t2.start();
112   }
113
114   /**
115    * the test which checks the synchronized method attribute
116    */

117   public void testSyncMthDeadlock () {
118     SyncRunnable r1 = new SyncMthRunnable();
119     SyncRunnable r2 = new SyncMthRunnable();
120     r1.setOther(r2);
121     r2.setOther(r1);
122
123     Thread JavaDoc t1 = new Thread JavaDoc(r1);
124     Thread JavaDoc t2 = new Thread JavaDoc(r2);
125
126     t1.start();
127     t2.start();
128   }
129
130   /**
131    * Required by the testMissedSignalDeadlock().
132    */

133   class Event {
134     int count = 0;
135
136     public synchronized void signal_event () {
137       count = (count + 1) % 3;
138       notifyAll();
139     }
140
141     public synchronized void wait_for_event () {
142       try {
143         wait();
144       } catch (InterruptedException JavaDoc e) {
145       }
146     }
147   }
148
149   /**
150    * Required by the testMissedSignalDeadlock().
151    */

152   class FirstTask extends java.lang.Thread JavaDoc {
153     Event event1;
154     Event event2;
155     int count = 0;
156
157     public FirstTask (Event e1, Event e2) {
158       event1 = e1;
159       event2 = e2;
160     }
161
162     public void run () {
163       count = event1.count;
164
165       while (true) {
166         if (count == event1.count) {
167           event1.wait_for_event();
168         }
169
170         count = event1.count;
171         event2.signal_event();
172       }
173     }
174   }
175
176   /**
177    * Required by the testMissedSignalDeadlock().
178    */

179   class SecondTask extends java.lang.Thread JavaDoc {
180     Event event1;
181     Event event2;
182     int count = 0;
183
184     public SecondTask (Event e1, Event e2) {
185       event1 = e1;
186       event2 = e2;
187     }
188
189     public void run () {
190       count = event2.count;
191
192       while (true) {
193         event1.signal_event();
194
195         if (count == event2.count) {
196           event2.wait_for_event();
197         }
198
199         count = event2.count;
200       }
201     }
202   }
203
204   /**
205    * DOCUMENT ME!
206    */

207   class SyncBlockRunnable extends SyncRunnable {
208     public void doSomething () {
209       synchronized (this) {
210         other.doSomethingElse();
211       }
212     }
213
214     public void doSomethingElse () {
215       synchronized (this) {
216       }
217     }
218
219     public void run () {
220       while (true) {
221         synchronized (this) {
222           other.doSomething();
223         }
224       }
225     }
226   }
227
228   /**
229    * DOCUMENT ME!
230    */

231   class SyncMthRunnable extends SyncRunnable {
232     public synchronized void doSomething () {
233       other.doSomethingElse();
234     }
235
236     public synchronized void doSomethingElse () {
237     }
238
239     public synchronized void run () {
240       while (true) {
241         other.doSomething();
242       }
243     }
244   }
245 }
Popular Tags