KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > concurrent > MonitorUtilsTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc {
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 JavaDoc lock = new Object JavaDoc();
25     final SynchronizedRef ref = new SynchronizedRef(null);
26
27     Thread JavaDoc t = new Thread JavaDoc() {
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 JavaDoc 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 JavaDoc) ref.get());
77     }
78
79   }
80
81   void log(String JavaDoc msg) {
82     System.out.println(System.currentTimeMillis() + " [" + Thread.currentThread().getName() + "] " + msg);
83   }
84
85 }
86
Popular Tags