KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > TransparentListApp


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 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 JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Random JavaDoc;
17
18 /**
19  * @author steve
20  */

21 public class TransparentListApp extends AbstractTransparentApp {
22   private final static int ACTION_COUNT = 5;
23
24   private String JavaDoc putterName;
25   private List JavaDoc queue = new LinkedList JavaDoc();
26   private Random JavaDoc random = new Random JavaDoc();
27
28   public TransparentListApp(String JavaDoc 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 JavaDoc 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 JavaDoc 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       //
54
}
55   }
56
57   private void put(Action action) {
58     synchronized (queue) {
59       queue.add(action);
60     }
61   }
62
63   private boolean execute(String JavaDoc 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 JavaDoc putter;
75
76     public Action(int count, String JavaDoc putter) {
77       this.count = count;
78       this.putter = putter;
79     }
80
81     public void execute(String JavaDoc executerName) {
82       System.out.println("*** Executing: Count:" + count + " putter:" + putter + " executer:" + executerName);
83     }
84
85     public String JavaDoc toString() {
86       return "Count:" + count + " putter:" + putter;
87     }
88   }
89
90 }
Popular Tags