01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tctest;
05:
06: import com.tc.object.config.ConfigVisitor;
07: import com.tc.object.config.DSOClientConfigHelper;
08: import com.tc.object.config.TransparencyClassSpec;
09: 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: /**
19: * @author steve
20: */
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,
29: ListenerProvider listenerProvider) {
30: super (appId, cfg, listenerProvider);
31: this .putterName = "TransparentListApp.putter(" + appId + ")";
32: }
33:
34: public static void visitL1DSOConfig(ConfigVisitor visitor,
35: DSOClientConfigHelper config) {
36: String testClassName = TransparentListApp.class.getName();
37: TransparencyClassSpec spec = config
38: .getOrCreateSpec(testClassName);
39: spec.addRoot("queue", "sharedQueue");
40: testClassName = TransparentListApp.Action.class.getName();
41: spec = config.getOrCreateSpec(testClassName);
42:
43: String methodExpression = "void com.tctest.TransparentListApp.put(com.tctest.TransparentListApp$Action)";
44: config.addWriteAutolock(methodExpression);
45: methodExpression = "boolean com.tctest.TransparentListApp.execute(java.lang.String)";
46: config.addWriteAutolock(methodExpression);
47:
48: }
49:
50: public void run() {
51: for (int i = 0; i < ACTION_COUNT; i++) {
52: put(new Action(i, putterName));
53: ThreadUtil.reallySleep(random.nextInt(100));
54: }
55: while (execute(putterName)) {
56: //
57: }
58: }
59:
60: private void put(Action action) {
61: synchronized (queue) {
62: queue.add(action);
63: }
64: }
65:
66: private boolean execute(String executerName) {
67: synchronized (queue) {
68: if (queue.size() == 0) {
69: return false;
70: }
71: Action action = (Action) queue.remove(0);
72: action.execute(executerName);
73: return true;
74: }
75: }
76:
77: public static class Action {
78: private int count;
79: private String putter;
80:
81: public Action(int count, String putter) {
82: this .count = count;
83: this .putter = putter;
84: }
85:
86: public void execute(String executerName) {
87: System.out
88: .println("*** Executing: Count:" + count
89: + " putter:" + putter + " executer:"
90: + executerName);
91: }
92:
93: public String toString() {
94: return "Count:" + count + " putter:" + putter;
95: }
96: }
97:
98: }
|