1 4 package com.tc.util.concurrent; 5 6 import EDU.oswego.cs.dl.util.concurrent.Latch; 7 import EDU.oswego.cs.dl.util.concurrent.SynchronizedRef; 8 9 import com.tc.test.TCTestCase; 10 import com.tc.util.runtime.Vm; 11 12 public class MonitorUtilsTest extends TCTestCase { 13 14 public void test() throws Exception { 15 if (Vm.isJRockit()) { 17 System.err.println("THIS TEST WORKS ONLY ON SUN VM"); 18 return; 19 } 20 21 Thread.currentThread().setName("main"); 22 final Latch latch = new Latch(); 23 final Latch latch2 = new Latch(); 24 final Object lock = new Object (); 25 final SynchronizedRef ref = new SynchronizedRef(null); 26 27 Thread t = new Thread () { 28 public void run() { 29 setName("thread"); 30 log("started"); 31 try { 32 synchronized (lock) { 33 log("monitor acquired 1"); 34 synchronized (lock) { 35 log("monitor acquired 2"); 36 synchronized (lock) { 37 log("monitor acquired 3"); 38 latch.release(); 39 log("about to sleep"); 40 ThreadUtil.reallySleep(10000); 41 log("done sleeping, about to release monitor"); 42 int count = MonitorUtils.releaseMonitor(lock); 43 log("release count was " + count); 44 latch2.acquire(); 45 log("re-acquiring lock"); 46 MonitorUtils.monitorEnter(lock, count); 47 log("lock re-acquired"); 48 } 49 log("left block 1"); 50 } 51 log("left block 2"); 52 } 53 log("left block 3"); 54 } catch (Throwable ex) { 55 ref.set(ex); 56 } 57 } 58 }; 59 t.start(); 60 61 log("waiting for thread"); 62 latch.acquire(); 63 log("signaled"); 64 65 synchronized (lock) { 66 log("got the lock"); 67 latch2.release(); 68 log("released"); 69 } 70 71 log("joining thread"); 72 t.join(); 73 log("thread dead"); 74 75 if (ref.get() != null) { 76 fail((Throwable ) ref.get()); 77 } 78 79 } 80 81 void log(String msg) { 82 System.out.println(System.currentTimeMillis() + " [" + Thread.currentThread().getName() + "] " + msg); 83 } 84 85 } 86 | Popular Tags |