1 4 package dso; 5 6 import java.util.HashMap ; 7 import java.util.LinkedList ; 8 import java.util.List ; 9 import java.util.Map ; 10 11 14 public class SimpleSharedQueueExample { 15 private final static int MAX_RESULTS_SIZE = 15; 16 private String putterName; 17 private List workQueue = new LinkedList (); 18 private Map allWorkers = new HashMap (); 19 private List results = new LinkedList (); 20 private Map resultsSummary = new HashMap (); 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 (myID)); 29 } 30 Thread t = new WorkerThread(this, putterName); 31 t.setDaemon(true); 32 t.start(); 33 } 34 35 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 53 public String getPutterName() { 54 return putterName; 55 } 56 57 60 public void clearResults() { 61 synchronized (results) { 62 results.clear(); 63 } 64 } 65 66 71 public List getResults() { 72 synchronized (results) { 73 return new LinkedList (results); 74 } 75 } 76 77 public HashMap getResultsSummary() { 78 synchronized (resultsSummary) { 79 return new HashMap (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 executerName) { 118 synchronized (workQueue) { 119 while (workQueue.size() == 0) { 120 try { 121 workQueue.wait(); 122 } catch (InterruptedException e) { 123 throw new RuntimeException (e); 124 } 125 } 126 Action action = (Action) workQueue.remove(0); 127 String 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 putterName; 136 137 public Action(String 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 execute(String 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 e) { 159 return "Invalid calculation:" + number1 + "" + sign + number2; 160 } 161 } 162 163 public String 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 getPutterName() { 180 return putterName; 181 } 182 } 183 184 private static class ResultsSummary { 185 private String name; 186 private int putCount; 187 private int executeCount; 188 189 public String toString() { 190 return "<tr>\n<td>" + this.name + "</td><td>" + this.executeCount + "</td><td>" + this.putCount + "</td></tr>"; 191 } 192 193 public ResultsSummary(String 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 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 putterName; 222 private String executerName; 223 private float number1; 224 private float number2; 225 private char sign; 226 private String result; 227 228 public String toString() { 229 return "Created by: " + putterName + " Executed by: " + executerName + " equation ( " + number1 + " " + sign 230 + " " + number2 + " = " + result + " )"; 231 } 232 233 public Result(Action action, String executerName, String 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 getPutterName() { 243 return this.putterName; 244 } 245 246 public String getExecuterName() { 247 return this.executerName; 248 } 249 } 250 251 private static class WorkerThread extends Thread { 252 private String putterName; 253 private SimpleSharedQueueExample parent; 254 255 public WorkerThread(SimpleSharedQueueExample parent, String 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 |