1 package gov.nasa.jpf.mc; 20 21 abstract class SyncRunnable implements Runnable { 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 38 public class TestVMDeadlock { 39 public static void main (String [] args) { 40 TestVMDeadlock t = new TestVMDeadlock(); 41 42 if (args.length > 0) { 43 for (int i = 0; i < args.length; i++) { 45 String func = args[i]; 46 47 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 ("unknown test function"); 59 } 60 } 61 } else { 62 t.testSyncMthDeadlock(); 64 } 65 } 66 67 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 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 t1 = new Thread (r1); 92 Thread t2 = new Thread (r2); 93 94 t1.start(); 95 t2.start(); 96 } 97 98 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 t1 = new Thread (r1); 108 Thread t2 = new Thread (r2); 109 110 t1.start(); 111 t2.start(); 112 } 113 114 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 t1 = new Thread (r1); 124 Thread t2 = new Thread (r2); 125 126 t1.start(); 127 t2.start(); 128 } 129 130 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 e) { 145 } 146 } 147 } 148 149 152 class FirstTask extends java.lang.Thread { 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 179 class SecondTask extends java.lang.Thread { 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 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 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 |