KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > bean > SharedLockBean


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.tctest.spring.bean;
5
6 import com.tc.aspectwerkz.proxy.Uuid;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.List JavaDoc;
10
11 /**
12  * This class is testing shared lock behavior
13  * 1. Locking on shared object applies to the whole cluster
14  * 2. Trying to modify a shared objec outside a shared lock should raise exception
15  * 3. Shared value will not be propagated until execution thread exits the monitor
16  */

17 public class SharedLockBean implements Runnable JavaDoc, ISharedLock {
18   // shared variables
19
private List JavaDoc sharedVar = new ArrayList JavaDoc();
20   private Object JavaDoc sharedLock = new Object JavaDoc();
21 // private Object mutex = new Object();
22

23   // variables not shared
24
private transient List JavaDoc unsharedVar = new ArrayList JavaDoc();
25   private transient long localID = System.identityHashCode(this) + Uuid.newUuid();
26   private transient volatile int stepId = 0;
27   
28   public void start() {
29     Thread JavaDoc th = new Thread JavaDoc(this);
30     th.setDaemon(true);
31     th.start();
32   }
33   
34   public void moveToStep(int step) {
35     stepId = step;
36     try {
37       // give enough time for the blocking thread to take action
38
Thread.sleep(1000L);
39     } catch (Exception JavaDoc e) {e.printStackTrace();}
40
41 // XXX should rewrite that with wait/notify, for now just increased timeout
42
// synchronized(this.mutex) {
43
// this.mutex.notifyAll();
44
// }
45
}
46    
47   public void run() {
48     holdOnStep(10); // below is step 10
49
try {
50       sharedVar.add("ckpoint1-" + localID);
51     } catch (Exception JavaDoc e) {
52       // except an exception since not synchronized
53
unsharedVar.add("ckpoint1-" + localID);
54     }
55     holdOnStep(20); // below is step 20
56
synchronized(this.sharedLock) {
57       sharedVar.add("ckpoint2-" + localID);
58       holdOnStep(30); // below is step 30
59
} // shared value propagation point
60
holdOnStep(40); // below is step 40 and return
61
}
62   
63   private void holdOnStep(int step) {
64     while (stepId < step) {
65       try {
66         Thread.sleep(100L);
67       } catch (Exception JavaDoc e) {e.printStackTrace();}
68 // synchronized(this.mutex) {
69
// try {
70
// this.mutex.wait(100L);
71
// System.err.println("### SharedLockBean.holdOnStep() "+stepId);
72
// } catch (InterruptedException e) {
73
// // ignore
74
// System.err.println("### SharedLockBean.holdOnStep() INTERRUPTED "+stepId);
75
// }
76
// }
77
}
78   }
79
80   // XXX shouldn't this be locked?
81
public List JavaDoc getSharedVar() {
82     return sharedVar;
83   }
84
85   public List JavaDoc gethUnSharedVar() {
86     return unsharedVar;
87   }
88
89   public long getLocalID() {
90     return localID;
91   }
92 }
93
Popular Tags