KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > TestReader


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 java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Map JavaDoc;
9
10 /**
11  * Reads from the shared map and that's it
12  */

13 public class TestReader {
14
15   public final static int READ_COUNT = 2000;
16
17   private Map JavaDoc stuff = new HashMap JavaDoc();
18   private String JavaDoc name;
19
20   public TestReader(String JavaDoc name) {
21     this.name = name;
22   }
23
24   public void read() {
25     try {
26       int count = 0;
27       while (count++ < READ_COUNT) {
28         doARead();
29       }
30     } catch (StackOverflowError JavaDoc e) {
31       e.printStackTrace(System.out);
32       throw e;
33     }
34   }
35
36   public void doARead() {
37     synchronized (stuff) {
38       // System.out.println("begin Reading:" + name);
39
if (stuff.size() > 0 && (stuff.size() % 4) == 0) {
40         for (Iterator JavaDoc i = stuff.values().iterator(); i.hasNext();) {
41           i.next();
42         }
43       }
44     }
45     // System.out.println("DONE Reading:" + name);
46
}
47
48   protected String JavaDoc getName() {
49     return name;
50   }
51
52 }
53
Popular Tags