KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > GreedyLocksSystemTestApp


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;
5
6 import EDU.oswego.cs.dl.util.concurrent.BrokenBarrierException;
7 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
8
9 import com.tc.object.config.ConfigVisitor;
10 import com.tc.object.config.DSOClientConfigHelper;
11 import com.tc.object.config.TransparencyClassSpec;
12 import com.tc.object.config.spec.CyclicBarrierSpec;
13 import com.tc.simulator.app.ApplicationConfig;
14 import com.tc.simulator.listener.ListenerProvider;
15 import com.tctest.runner.AbstractTransparentApp;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19
20 public class GreedyLocksSystemTestApp extends AbstractTransparentApp {
21
22   public static final int NODE_COUNT = 3;
23   public static final int EXECUTION_COUNT = 5;
24   public static final int ITERATION_COUNT = 1;
25
26   private List JavaDoc locks = new ArrayList JavaDoc();
27   private CyclicBarrier barrier = new CyclicBarrier(NODE_COUNT * EXECUTION_COUNT);
28   private String JavaDoc name;
29   private static int id = 0;
30   private static int count = 5;
31
32   public GreedyLocksSystemTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
33     super(appId, cfg, listenerProvider);
34   }
35
36   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
37     String JavaDoc testClass = GreedyLocksSystemTestApp.class.getName();
38     String JavaDoc lockClass = GreedyLocksSystemTestApp.LockObject.class.getName();
39     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
40     config.addIncludePattern(lockClass);
41
42     String JavaDoc setIDMethodExpression = "* " + testClass + "*.setID(..)";
43
44     String JavaDoc readMethodExpression = "* " + lockClass + "*.read(..)";
45     String JavaDoc writeCountMethodExpression = "* " + lockClass + "*.getWriteCount(..)";
46     String JavaDoc timeoutCountMethodExpression = "* " + lockClass + "*.getTimeoutCount(..)";
47     String JavaDoc notifyCountMethodExpression = "* " + lockClass + "*.getNotifyCount(..)";
48     String JavaDoc writeMethodExpression = "* " + lockClass + "*.write(..)";
49     String JavaDoc waitNotifyMethodExpression = "* " + lockClass + "*.waitAndNotify(..)";
50
51     config.addWriteAutolock(setIDMethodExpression);
52     config.addReadAutolock(readMethodExpression);
53     config.addReadAutolock(writeCountMethodExpression);
54     config.addReadAutolock(timeoutCountMethodExpression);
55     config.addReadAutolock(notifyCountMethodExpression);
56     config.addWriteAutolock(writeMethodExpression);
57     config.addWriteAutolock(waitNotifyMethodExpression);
58     spec.addRoot("locks", "locks");
59     spec.addRoot("barrier", "barrier");
60     new CyclicBarrierSpec().visit(visitor, config);
61   }
62
63   public void run() {
64     name = Thread.currentThread().getName();
65
66     setID();
67
68     try {
69       barrier.barrier();
70     } catch (BrokenBarrierException e) {
71       e.printStackTrace();
72     } catch (InterruptedException JavaDoc e) {
73       e.printStackTrace();
74     }
75
76     for (int i = id - 1; i < locks.size(); i++) {
77       LockObject lock = (LockObject) locks.get(i);
78       println(" Working on " + lock);
79       int read = 0, write = 0;
80
81       // READ loop
82
for (int j = 0; j < count; j++) {
83         read = lock.read();
84       }
85       println(lock + ":: After Read loop :: " + read);
86
87       // Write loop
88
for (int j = 0; j < count; j++) {
89         write = lock.write();
90       }
91       println(lock + ": After Write loop :: " + write);
92
93       // Wait notify
94
for (int j = 0; j < count; j++) {
95         lock.waitAndNotify(toString());
96       }
97       println(lock + ": Timeouts :: " + lock.getTimeoutCount());
98       println(lock + ": Notifies :: " + lock.getNotifyCount());
99
100       // READ, WRITE and Wait Notify
101
for (int j = 0; j < count; j++) {
102         read = lock.read();
103         write = lock.write();
104         lock.waitAndNotify(toString());
105       }
106       println(lock + ":: Reads :: " + read);
107       println(lock + ":: Writes :: " + write);
108       println(lock + ": Timeouts :: " + lock.getTimeoutCount());
109       println(lock + ": Notifies :: " + lock.getNotifyCount());
110     }
111   }
112
113   private void println(String JavaDoc string) {
114     System.out.println(toString() + string);
115   }
116
117   public String JavaDoc toString() {
118     return "Client(" + id + ")::" + name + "::";
119   }
120
121   private void setID() {
122     synchronized (locks) {
123       if (id == 0) {
124         // first thread
125
id = locks.size() + 1;
126         locks.add(new LockObject(locks, id));
127         // count *= id;
128
}
129     }
130   }
131
132   private static class LockObject {
133
134     private List JavaDoc locks;
135     private int lockID;
136
137     int writeCount = 0;
138     int waitCount = 0;
139     int timeoutCount = 0;
140     int notifiedCount = 0;
141
142     LockObject(List JavaDoc list, int id) {
143       this.locks = list;
144       this.lockID = id;
145     }
146
147     synchronized int read() {
148       int c = writeCount;
149       for (int i = 0; i < locks.size(); i++) {
150         LockObject lock = (LockObject) locks.get(i);
151         if (lock != this) {
152           c += lock.getWriteCount();
153         }
154       }
155       return c;
156     }
157
158     synchronized void waitAndNotify(String JavaDoc name) {
159       waitCount++;
160       try {
161         long start = System.currentTimeMillis();
162         wait(500);
163         if ((System.currentTimeMillis() - start) >= 500) {
164           timeoutCount++;
165         } else {
166           notifiedCount++;
167         }
168       } catch (InterruptedException JavaDoc e) {
169         e.printStackTrace();
170       }
171       waitCount--;
172       switch (waitCount % 3) {
173         case 0:
174           break;
175         case 1:
176           notify();
177           break;
178         case 2:
179           notifyAll();
180       }
181     }
182
183     int getWriteCount() {
184       return writeCount;
185     }
186
187     int getTimeoutCount() {
188       return timeoutCount;
189     }
190
191     int getNotifyCount() {
192       return notifiedCount;
193     }
194
195     synchronized int write() {
196       int c = ++writeCount;
197       for (int i = 0; i < locks.size(); i++) {
198         LockObject lock = (LockObject) locks.get(i);
199         if (lock != this) {
200           c += lock.getWriteCount();
201         }
202       }
203       return c;
204     }
205
206     int getID() {
207       return lockID;
208     }
209
210     public String JavaDoc toString() {
211       return "LockObject(" + getID() + ")";
212     }
213   }
214
215 }
216
Popular Tags