KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > SingleVMWaitNotifyTestApp


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.SynchronizedLong;
7
8 import com.tc.object.config.ConfigVisitor;
9 import com.tc.object.config.DSOClientConfigHelper;
10 import com.tc.simulator.app.ApplicationConfig;
11 import com.tc.simulator.listener.ListenerProvider;
12 import com.tc.util.concurrent.ThreadUtil;
13 import com.tctest.runner.AbstractTransparentApp;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.LinkedList JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Random JavaDoc;
21
22 public class SingleVMWaitNotifyTestApp extends AbstractTransparentApp {
23   private static Random JavaDoc random = new Random JavaDoc();
24   private static int TAKERS = random.nextInt(7) + 5;
25   private static int PUTTERS = random.nextInt(7) + 5;
26
27   private static final SynchronizedLong takeCount = new SynchronizedLong(0);
28
29   protected static final int PUTS = 200;
30
31   // root
32
private final List JavaDoc queue = new LinkedList JavaDoc();
33
34   public SingleVMWaitNotifyTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
35     super(appId, cfg, listenerProvider);
36
37     if (getParticipantCount() != 1) { throw new RuntimeException JavaDoc("participant count must be 1"); }
38   }
39
40   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
41     String JavaDoc testClassName = SingleVMWaitNotifyTestApp.class.getName();
42     config.addIncludePattern(testClassName);
43     String JavaDoc root = "queue";
44     config.addRoot(testClassName, root, root + "Lock", true);
45     System.err.println("Adding root for " + testClassName + "." + root);
46
47     String JavaDoc methodExpression = "* " + testClassName + "*.*(..)";
48     System.err.println("Adding autolock for: " + methodExpression);
49     config.addWriteAutolock(methodExpression);
50
51     //config.addIncludePattern(Item.class.getName());
52
config.addIncludePattern(testClassName + "$*");
53   }
54
55   public void run() {
56     System.out.println("Number of putters: " + PUTTERS);
57     System.out.println("Number of takers: " + TAKERS);
58
59     ArrayList JavaDoc threads = new ArrayList JavaDoc();
60
61     for (int i = 0; i < PUTTERS; i++) {
62       threads.add(createPutter(i));
63     }
64
65     for (int i = 0; i < TAKERS; i++) {
66       threads.add(createTaker(i));
67     }
68
69     // randomize the thread positions in the array, such that they start at random times
70
Collections.shuffle(threads, random);
71     for (Iterator JavaDoc iter = threads.iterator(); iter.hasNext();) {
72       Thread JavaDoc t = (Thread JavaDoc) iter.next();
73       t.start();
74     }
75
76     final Thread JavaDoc me = Thread.currentThread();
77     Thread JavaDoc timeout = new Thread JavaDoc(new Runnable JavaDoc() {
78       public void run() {
79         ThreadUtil.reallySleep(1000 * 60 * 5);
80         me.interrupt();
81       }
82     });
83     timeout.setDaemon(true);
84     timeout.start();
85
86     waitForThreads(threads, true);
87
88     // queue up the stop messages for the takers
89
synchronized (queue) {
90       for (int i = 0; i < TAKERS; i++) {
91         queue.add(new Item(true));
92       }
93       queue.notifyAll();
94     }
95
96     // wait for everyone to finish (should just be the takers)
97
waitForThreads(threads, false);
98
99     final long expected = PUTTERS * PUTS;
100     final long actual = takeCount.get();
101     if (expected != actual) { throw new RuntimeException JavaDoc(actual + " != " + expected); }
102
103     notifyResult(Boolean.TRUE);
104   }
105
106   private void waitForThreads(ArrayList JavaDoc threads, boolean justPutters) {
107     for (Iterator JavaDoc iter = threads.iterator(); iter.hasNext();) {
108       Thread JavaDoc t = (Thread JavaDoc) iter.next();
109
110       if (!justPutters || t.getName().startsWith("PUTTER")) {
111         try {
112           t.join();
113         } catch (InterruptedException JavaDoc e) {
114           e.printStackTrace();
115           throw new RuntimeException JavaDoc(e);
116         }
117       }
118     }
119   }
120
121   private Thread JavaDoc createTaker(int i) {
122     Thread JavaDoc rv = new Thread JavaDoc(new Runnable JavaDoc() {
123       public void run() {
124         try {
125           synchronized (queue) {
126             while (true) {
127               if (queue.size() > 0) {
128                 Item item = (Item) queue.remove(0);
129                 if (item.stop) { return; }
130                 takeCount.increment();
131               } else {
132                 try {
133                   queue.wait();
134                 } catch (InterruptedException JavaDoc e) {
135                   throw new RuntimeException JavaDoc(e);
136                 }
137               }
138             }
139           }
140         } catch (Throwable JavaDoc t) {
141           notifyError(t);
142         }
143       }
144     }, "TAKER " + i);
145
146     rv.setDaemon(true);
147     return rv;
148   }
149
150   private Thread JavaDoc createPutter(final int id) {
151     Thread JavaDoc rv = new Thread JavaDoc( new Runnable JavaDoc() {
152       public void run() {
153         try {
154           for (int i = 0; i < PUTS; i++) {
155             synchronized (queue) {
156               queue.add(new Item());
157               queue.notifyAll();
158             }
159           }
160         } catch (Throwable JavaDoc t) {
161           notifyError(t);
162         }
163       }
164     }, "PUTTER " + id);
165
166     rv.setDaemon(true);
167     return rv;
168   }
169
170   static class Item {
171     private final boolean stop;
172
173     Item() {
174       this(false);
175     }
176
177     Item(boolean stop) {
178       this.stop = stop;
179     }
180
181     boolean isStop() {
182       return this.stop;
183     }
184
185   }
186
187 }
Popular Tags