1 4 package com.tctest; 5 6 import com.tc.object.config.ConfigVisitor; 7 import com.tc.object.config.DSOClientConfigHelper; 8 import com.tc.object.config.TransparencyClassSpec; 9 import com.tc.simulator.app.ApplicationConfig; 10 import com.tc.simulator.listener.ListenerProvider; 11 import com.tc.util.concurrent.ThreadUtil; 12 import com.tctest.runner.AbstractTransparentApp; 13 14 import java.util.LinkedList ; 15 import java.util.List ; 16 import java.util.Random ; 17 18 21 public class TransparentListApp extends AbstractTransparentApp { 22 private final static int ACTION_COUNT = 5; 23 24 private String putterName; 25 private List queue = new LinkedList (); 26 private Random random = new Random (); 27 28 public TransparentListApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 29 super(appId, cfg, listenerProvider); 30 this.putterName = "TransparentListApp.putter(" + appId + ")"; 31 } 32 33 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 34 String testClassName = TransparentListApp.class.getName(); 35 TransparencyClassSpec spec = config.getOrCreateSpec(testClassName); 36 spec.addRoot("queue", "sharedQueue"); 37 testClassName = TransparentListApp.Action.class.getName(); 38 spec = config.getOrCreateSpec(testClassName); 39 40 String methodExpression = "void com.tctest.TransparentListApp.put(com.tctest.TransparentListApp$Action)"; 41 config.addWriteAutolock(methodExpression); 42 methodExpression = "boolean com.tctest.TransparentListApp.execute(java.lang.String)"; 43 config.addWriteAutolock(methodExpression); 44 45 } 46 47 public void run() { 48 for (int i = 0; i < ACTION_COUNT; i++) { 49 put(new Action(i, putterName)); 50 ThreadUtil.reallySleep(random.nextInt(100)); 51 } 52 while (execute(putterName)) { 53 } 55 } 56 57 private void put(Action action) { 58 synchronized (queue) { 59 queue.add(action); 60 } 61 } 62 63 private boolean execute(String executerName) { 64 synchronized (queue) { 65 if (queue.size() == 0) { return false; } 66 Action action = (Action) queue.remove(0); 67 action.execute(executerName); 68 return true; 69 } 70 } 71 72 public static class Action { 73 private int count; 74 private String putter; 75 76 public Action(int count, String putter) { 77 this.count = count; 78 this.putter = putter; 79 } 80 81 public void execute(String executerName) { 82 System.out.println("*** Executing: Count:" + count + " putter:" + putter + " executer:" + executerName); 83 } 84 85 public String toString() { 86 return "Count:" + count + " putter:" + putter; 87 } 88 } 89 90 } | Popular Tags |