01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.util.concurrent;
05:
06: import EDU.oswego.cs.dl.util.concurrent.Latch;
07: import EDU.oswego.cs.dl.util.concurrent.SynchronizedRef;
08:
09: 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: // this test is Sun VM only, of course this condition only exludes JRockit, but that ought to do it for now
16: 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
43: .releaseMonitor(lock);
44: log("release count was " + count);
45: latch2.acquire();
46: log("re-acquiring lock");
47: MonitorUtils.monitorEnter(lock, count);
48: log("lock re-acquired");
49: }
50: log("left block 1");
51: }
52: log("left block 2");
53: }
54: log("left block 3");
55: } catch (Throwable ex) {
56: ref.set(ex);
57: }
58: }
59: };
60: t.start();
61:
62: log("waiting for thread");
63: latch.acquire();
64: log("signaled");
65:
66: synchronized (lock) {
67: log("got the lock");
68: latch2.release();
69: log("released");
70: }
71:
72: log("joining thread");
73: t.join();
74: log("thread dead");
75:
76: if (ref.get() != null) {
77: fail((Throwable) ref.get());
78: }
79:
80: }
81:
82: void log(String msg) {
83: System.out.println(System.currentTimeMillis() + " ["
84: + Thread.currentThread().getName() + "] " + msg);
85: }
86:
87: }
|