1 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 ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 public class ConcentratedClassTestApp extends AbstractTransparentApp { 23 private static final int THREAD_COUNT = 2; 24 private static Map root = new HashMap (); 25 26 public ConcentratedClassTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 27 super(appId, cfg, listenerProvider); 28 if (getParticipantCount() < 2) { 29 throw new RuntimeException ("Need at least 2 participants for this test"); 32 } 33 } 34 35 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 36 String testClass = ConcentratedClassTestApp.class.getName(); 37 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 38 String 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 id, int count) { 46 ConcentratedClass cc = new ConcentratedClass(); 47 48 synchronized (root) { 49 final Object prev = root.put(id, cc); 50 if (prev != null) { throw new RuntimeException ("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 e) { 59 throw new RuntimeException (e); 60 } 61 } 62 } 63 } 64 65 Collection values; 66 synchronized (root) { 67 values = root.values(); 68 } 69 70 Set all = new HashSet (); 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 ("unexpected size: " + values.size()); } 83 84 all.add(prevCC); 85 for (Iterator 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 id = getApplicationId(); 95 96 Thread threads[] = new Thread [THREAD_COUNT]; 97 for (int i = 0; i < threads.length; i++) { 98 final int cnt = i; 99 threads[i] = new Thread (new Runnable () { 100 public void run() { 101 try { 102 threadEntry(id + cnt, getParticipantCount() * THREAD_COUNT); 103 } catch (Throwable 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 e) { 118 throw new RuntimeException (e); 119 } 120 } 121 122 Throwable t = (Throwable ) error.get(); 123 if (t != null) { throw new RuntimeException (t); } 124 } 125 126 private static class ConcentratedClass { 127 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 |