KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dso > SimpleSharedQueueExample


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 dso;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.LinkedList JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10
11 /**
12  * @author steve
13  */

14 public class SimpleSharedQueueExample {
15   private final static int MAX_RESULTS_SIZE = 15;
16   private String JavaDoc putterName;
17   private List JavaDoc workQueue = new LinkedList JavaDoc();
18   private Map JavaDoc allWorkers = new HashMap JavaDoc();
19   private List JavaDoc results = new LinkedList JavaDoc();
20   //ARI: Keep summary info as we add results so that we can print a pretty
21
// summary
22
private Map JavaDoc resultsSummary = new HashMap JavaDoc();
23
24   public SimpleSharedQueueExample() {
25     synchronized (allWorkers) {
26       int myID = allWorkers.size() + 1;
27       this.putterName = "Worker:" + myID;
28       this.allWorkers.put(putterName, new Integer JavaDoc(myID));
29     }
30     Thread JavaDoc t = new WorkerThread(this, putterName);
31     t.setDaemon(true);
32     t.start();
33   }
34
35   /**
36    * Add a calculation to the work queue to be picked up later by what ever node gets it first and executed.
37    *
38    * @param number1 - first item in the calculation
39    * @param number2 - second item in the calculation
40    * @param sign - calculation to perform
41    */

42   public void addAction(float number1, float number2, char sign, int timesRepeat) {
43     for (int i = 0; i < timesRepeat; i++) {
44       put(new Action(putterName, number1, number2, sign));
45     }
46   }
47
48   /**
49    * The name of this node.
50    *
51    * @return
52    */

53   public String JavaDoc getPutterName() {
54     return putterName;
55   }
56
57   /**
58    * remove all the results from the results collection
59    */

60   public void clearResults() {
61     synchronized (results) {
62       results.clear();
63     }
64   }
65
66   /**
67    * Get all the results so far from all nodes and return them
68    *
69    * @return List of results
70    */

71   public List JavaDoc getResults() {
72     synchronized (results) {
73       return new LinkedList JavaDoc(results);
74     }
75   }
76
77   public HashMap JavaDoc getResultsSummary() {
78     synchronized (resultsSummary) {
79       return new HashMap JavaDoc(resultsSummary);
80     }
81   }
82
83   private void addResultSummary(Result result) {
84     synchronized (resultsSummary) {
85       ResultsSummary rs = null;
86       if (!resultsSummary.containsKey(result.getPutterName())) {
87         resultsSummary.put(result.getPutterName(), new ResultsSummary(result.getPutterName(), 0, 0));
88       }
89       rs = (ResultsSummary) resultsSummary.get(result.getPutterName());
90       rs.incrementPutCount();
91
92       if (!resultsSummary.containsKey(result.getExecuterName())) {
93         resultsSummary.put(result.getExecuterName(), new ResultsSummary(result.getExecuterName(), 0, 0));
94       }
95       rs = (ResultsSummary) resultsSummary.get(result.getExecuterName());
96       rs.incrementExecuteCount();
97     }
98   }
99
100   private void addResult(Result result) {
101     synchronized (results) {
102       results.add(result.toString());
103       if (results.size() > MAX_RESULTS_SIZE) {
104         results.remove(0);
105       }
106     }
107     addResultSummary(result);
108   }
109
110   private void put(Action action) {
111     synchronized (workQueue) {
112       workQueue.add(action);
113       workQueue.notifyAll();
114     }
115   }
116
117   private Result execute(String JavaDoc executerName) {
118     synchronized (workQueue) {
119       while (workQueue.size() == 0) {
120         try {
121           workQueue.wait();
122         } catch (InterruptedException JavaDoc e) {
123           throw new RuntimeException JavaDoc(e);
124         }
125       }
126       Action action = (Action) workQueue.remove(0);
127       String JavaDoc result = action.execute(executerName);
128       return new Result(action, executerName, result);
129     }
130   }
131
132   public static class Action {
133     private float number1, number2;
134     private char sign;
135     private String JavaDoc putterName;
136
137     public Action(String JavaDoc putter, float number1, float number2, char sign) {
138       this.putterName = putter;
139       this.number1 = number1;
140       this.number2 = number2;
141       this.sign = sign;
142     }
143
144     public String JavaDoc execute(String JavaDoc executerName) {
145       try {
146         switch (sign) {
147           case '+':
148             return "" + (number1 + number2);
149           case '-':
150             return "" + (number1 - number2);
151           case '*':
152             return "" + (number1 * number2);
153           case '/':
154             return "" + (number1 / number2);
155           default:
156             return "error:" + sign;
157         }
158       } catch (Exception JavaDoc e) {
159         return "Invalid calculation:" + number1 + "" + sign + number2;
160       }
161     }
162
163     public String JavaDoc toString() {
164       return "Sign:" + sign + " putter:" + putterName + " number1:" + number1 + " number2:" + number2;
165     }
166
167     public float getNumber1() {
168       return number1;
169     }
170
171     public float getNumber2() {
172       return number2;
173     }
174
175     public char getSign() {
176       return sign;
177     }
178
179     public String JavaDoc getPutterName() {
180       return putterName;
181     }
182   }
183
184   private static class ResultsSummary {
185     private String JavaDoc name;
186     private int putCount;
187     private int executeCount;
188
189     public String JavaDoc toString() {
190       return "<tr>\n<td>" + this.name + "</td><td>" + this.executeCount + "</td><td>" + this.putCount + "</td></tr>";
191     }
192
193     public ResultsSummary(String JavaDoc name, int p, int e) {
194       this.name = name;
195       this.putCount = p;
196       this.executeCount = e;
197     }
198
199     public void incrementExecuteCount() {
200       this.executeCount++;
201     }
202
203     public void incrementPutCount() {
204       this.putCount++;
205     }
206
207     public String JavaDoc getName() {
208       return this.name;
209     }
210
211     public int getExecuteCount() {
212       return this.executeCount;
213     }
214
215     public int getPutCount() {
216       return this.putCount;
217     }
218   }
219
220   private static class Result {
221     private String JavaDoc putterName;
222     private String JavaDoc executerName;
223     private float number1;
224     private float number2;
225     private char sign;
226     private String JavaDoc result;
227
228     public String JavaDoc toString() {
229       return "Created by: " + putterName + " Executed by: " + executerName + " equation ( " + number1 + " " + sign
230              + " " + number2 + " = " + result + " )";
231     }
232
233     public Result(Action action, String JavaDoc executerName, String JavaDoc result) {
234       this.putterName = action.getPutterName();
235       this.executerName = executerName;
236       this.number1 = action.getNumber1();
237       this.number2 = action.getNumber2();
238       this.sign = action.getSign();
239       this.result = result;
240     }
241
242     public String JavaDoc getPutterName() {
243       return this.putterName;
244     }
245
246     public String JavaDoc getExecuterName() {
247       return this.executerName;
248     }
249   }
250
251   private static class WorkerThread extends Thread JavaDoc {
252     private String JavaDoc putterName;
253     private SimpleSharedQueueExample parent;
254
255     public WorkerThread(SimpleSharedQueueExample parent, String JavaDoc putterName) {
256       this.parent = parent;
257       this.putterName = putterName;
258     }
259
260     public void run() {
261       while (true) {
262         Result result = null;
263         result = parent.execute(putterName);
264         if (result != null) {
265           parent.addResult(result);
266         }
267       }
268     }
269   }
270 }
Popular Tags