1 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 ; 16 import java.util.Collections ; 17 import java.util.Iterator ; 18 import java.util.LinkedList ; 19 import java.util.List ; 20 import java.util.Random ; 21 22 public class SingleVMWaitNotifyTestApp extends AbstractTransparentApp { 23 private static Random random = new Random (); 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 private final List queue = new LinkedList (); 33 34 public SingleVMWaitNotifyTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 35 super(appId, cfg, listenerProvider); 36 37 if (getParticipantCount() != 1) { throw new RuntimeException ("participant count must be 1"); } 38 } 39 40 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 41 String testClassName = SingleVMWaitNotifyTestApp.class.getName(); 42 config.addIncludePattern(testClassName); 43 String root = "queue"; 44 config.addRoot(testClassName, root, root + "Lock", true); 45 System.err.println("Adding root for " + testClassName + "." + root); 46 47 String methodExpression = "* " + testClassName + "*.*(..)"; 48 System.err.println("Adding autolock for: " + methodExpression); 49 config.addWriteAutolock(methodExpression); 50 51 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 threads = new ArrayList (); 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 Collections.shuffle(threads, random); 71 for (Iterator iter = threads.iterator(); iter.hasNext();) { 72 Thread t = (Thread ) iter.next(); 73 t.start(); 74 } 75 76 final Thread me = Thread.currentThread(); 77 Thread timeout = new Thread (new Runnable () { 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 synchronized (queue) { 90 for (int i = 0; i < TAKERS; i++) { 91 queue.add(new Item(true)); 92 } 93 queue.notifyAll(); 94 } 95 96 waitForThreads(threads, false); 98 99 final long expected = PUTTERS * PUTS; 100 final long actual = takeCount.get(); 101 if (expected != actual) { throw new RuntimeException (actual + " != " + expected); } 102 103 notifyResult(Boolean.TRUE); 104 } 105 106 private void waitForThreads(ArrayList threads, boolean justPutters) { 107 for (Iterator iter = threads.iterator(); iter.hasNext();) { 108 Thread t = (Thread ) iter.next(); 109 110 if (!justPutters || t.getName().startsWith("PUTTER")) { 111 try { 112 t.join(); 113 } catch (InterruptedException e) { 114 e.printStackTrace(); 115 throw new RuntimeException (e); 116 } 117 } 118 } 119 } 120 121 private Thread createTaker(int i) { 122 Thread rv = new Thread (new Runnable () { 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 e) { 135 throw new RuntimeException (e); 136 } 137 } 138 } 139 } 140 } catch (Throwable t) { 141 notifyError(t); 142 } 143 } 144 }, "TAKER " + i); 145 146 rv.setDaemon(true); 147 return rv; 148 } 149 150 private Thread createPutter(final int id) { 151 Thread rv = new Thread ( new Runnable () { 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 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
|