KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > ConcentratedClassTestApp


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.SynchronizedRef;
7
8 import com.tc.object.config.ConfigVisitor;
9 import com.tc.object.config.DSOClientConfigHelper;
10 import com.tc.object.config.TransparencyClassSpec;
11 import com.tc.simulator.app.ApplicationConfig;
12 import com.tc.simulator.listener.ListenerProvider;
13 import com.tctest.runner.AbstractTransparentApp;
14
15 import java.util.Collection JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 public class ConcentratedClassTestApp extends AbstractTransparentApp {
23   private static final int THREAD_COUNT = 2;
24   private static Map JavaDoc root = new HashMap JavaDoc();
25
26   public ConcentratedClassTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
27     super(appId, cfg, listenerProvider);
28     if (getParticipantCount() < 2) {
29       // this test needs to have transactions coming in from other nodes (such that reflection based field set()'ing
30
// occurs as DNA is applied locally
31
throw new RuntimeException JavaDoc("Need at least 2 participants for this test");
32     }
33   }
34
35   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
36     String JavaDoc testClass = ConcentratedClassTestApp.class.getName();
37     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
38     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
39     config.addWriteAutolock(methodExpression);
40
41     spec.addRoot("root", "lock");
42     config.addIncludePattern(ConcentratedClass.class.getName());
43   }
44
45   private void threadEntry(String JavaDoc id, int count) {
46     ConcentratedClass cc = new ConcentratedClass();
47
48     synchronized (root) {
49       final Object JavaDoc prev = root.put(id, cc);
50       if (prev != null) { throw new RuntimeException JavaDoc("replaced an entry in the map for id " + id); }
51
52       if (root.size() == count) {
53         root.notifyAll();
54       } else {
55         while (root.size() != count) {
56           try {
57             root.wait();
58           } catch (InterruptedException JavaDoc e) {
59             throw new RuntimeException JavaDoc(e);
60           }
61         }
62       }
63     }
64
65     Collection JavaDoc values;
66     synchronized (root) {
67       values = root.values();
68     }
69
70     Set JavaDoc all = new HashSet JavaDoc();
71
72     for (int i = 0; i < 10; i++) {
73       final ConcentratedClass prevCC;
74       final ConcentratedClass newCC = new ConcentratedClass();
75       all.add(newCC);
76
77       synchronized (root) {
78         prevCC = (ConcentratedClass) root.put(id, newCC);
79         values = root.values();
80       }
81
82       if (values.size() != count) { throw new RuntimeException JavaDoc("unexpected size: " + values.size()); }
83
84       all.add(prevCC);
85       for (Iterator JavaDoc iter = all.iterator(); iter.hasNext();) {
86         ConcentratedClass someCC = (ConcentratedClass) iter.next();
87         someCC.increment();
88       }
89     }
90   }
91
92   public void run() {
93     final SynchronizedRef error = new SynchronizedRef(null);
94     final String JavaDoc id = getApplicationId();
95
96     Thread JavaDoc threads[] = new Thread JavaDoc[THREAD_COUNT];
97     for (int i = 0; i < threads.length; i++) {
98       final int cnt = i;
99       threads[i] = new Thread JavaDoc(new Runnable JavaDoc() {
100         public void run() {
101           try {
102             threadEntry(id + cnt, getParticipantCount() * THREAD_COUNT);
103           } catch (Throwable JavaDoc t) {
104             error.set(t);
105           }
106         }
107       });
108     }
109
110     for (int i = 0; i < threads.length; i++) {
111       threads[i].start();
112     }
113
114     for (int i = 0; i < threads.length; i++) {
115       try {
116         threads[i].join(5000);
117       } catch (InterruptedException JavaDoc e) {
118         throw new RuntimeException JavaDoc(e);
119       }
120     }
121
122     Throwable JavaDoc t = (Throwable JavaDoc) error.get();
123     if (t != null) { throw new RuntimeException JavaDoc(t); }
124   }
125
126   private static class ConcentratedClass {
127     // Leave these fields private! This test is trying to shake out concurrency problems set/get()'ing private
128
// fields via reflection in GenericTCField
129
private int counter1 = 0;
130     private int counter2 = 0;
131
132     synchronized int increment() {
133       counter1++;
134       counter2++;
135       return counter1;
136     }
137   }
138
139 }
Popular Tags