KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > LinkedQueueTestApp


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.LinkedQueue;
7 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
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.LinkedQueueSpec;
13 import com.tc.object.config.spec.SynchronizedIntSpec;
14 import com.tc.simulator.app.ApplicationConfig;
15 import com.tc.simulator.listener.ListenerProvider;
16 import com.tctest.runner.AbstractTransparentApp;
17
18 import java.util.Random JavaDoc;
19
20 public class LinkedQueueTestApp extends AbstractTransparentApp {
21
22   public static int COUNT = 500;
23   public static int DEBUG_COUNT = 100;
24
25   private LinkedQueue queue = new LinkedQueue();
26   //private List queue = new ArrayList();
27
private SynchronizedInt in = new SynchronizedInt(0);
28   private SynchronizedInt out = new SynchronizedInt(0);
29
30   public LinkedQueueTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
31     super(appId, cfg, listenerProvider);
32   }
33
34   public void run() {
35     Random JavaDoc random = new Random JavaDoc();
36     while (out.get() < COUNT) {
37       if ((random.nextInt(2) == 0) || (in.get() >= COUNT)) {
38           get();
39       } else {
40           put();
41       }
42     }
43   }
44
45   private void get() {
46     synchronized (out) {
47       try {
48       if (!queue.isEmpty()) {
49         Integer JavaDoc i = (Integer JavaDoc) queue.take();
50         //Integer i = (Integer) take(queue);
51
if (i.intValue() != out.increment()) {
52           throw new AssertionError JavaDoc(" Got = " + i.intValue() + " and Expected = " + out.get());
53         }
54         //if ((i.intValue() % DEBUG_COUNT) == 0) {
55
println(" Got : " + i);
56         //}
57
}
58       } catch (InterruptedException JavaDoc e) {
59       throw new AssertionError JavaDoc(e);
60       }
61     }
62
63   }
64
65   private void put() {
66     synchronized (in) {
67       try {
68       Integer JavaDoc i = new Integer JavaDoc(in.increment());
69       queue.put(i);
70       //put(queue, i);
71
println("Put : " + i);
72       } catch (InterruptedException JavaDoc e) {
73       throw new AssertionError JavaDoc(e);
74       }
75     }
76   }
77
78   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
79
80     String JavaDoc testClassName = LinkedQueueTestApp.class.getName();
81     TransparencyClassSpec spec = config.getOrCreateSpec(testClassName);
82
83     // Create Roots
84
spec.addRoot("queue", testClassName + ".queue");
85     spec.addRoot("in", testClassName + ".in");
86     spec.addRoot("out", testClassName + ".out");
87
88     // Create locks
89
String JavaDoc runExpression = "* " + testClassName + ".*(..)";
90     System.err.println("Adding write autolock for: " + runExpression);
91     config.addWriteAutolock(runExpression);
92
93     new SynchronizedIntSpec().visit(visitor, config);
94
95     new LinkedQueueSpec().visit(visitor, config);
96   }
97
98 // private static Object take(List workQueue2) {
99
// synchronized (workQueue2) {
100
// while (workQueue2.size() == 0) {
101
// try {
102
// workQueue2.wait();
103
// } catch (InterruptedException e) {
104
// throw new RuntimeException(e);
105
// }
106
// }
107
// return workQueue2.remove(0);
108
// }
109
// }
110
//
111
// private static void put(List workQueue2, Object o) {
112
// synchronized (workQueue2) {
113
// workQueue2.add(o);
114
// workQueue2.notify();
115
// }
116
// }
117

118   private static void println(Object JavaDoc o) {
119     System.err.println(Thread.currentThread().getName() + " : " + String.valueOf(o));
120   }
121
122 }
123
Popular Tags