01: /*
02: * Created on Aug 12, 2004 by pjacob
03: *
04: */
05: package com.whirlycott.cache.test;
06:
07: import org.apache.commons.logging.Log;
08: import org.apache.commons.logging.LogFactory;
09:
10: import junit.framework.TestCase;
11:
12: import com.whirlycott.cache.Cache;
13: import com.whirlycott.cache.CacheException;
14: import com.whirlycott.cache.CacheManager;
15:
16: /**
17: * @author pjacob
18: *
19: */
20: public class MultiThreadedTest extends TestCase {
21:
22: private static Log log = LogFactory.getLog(MultiThreadedTest.class);
23:
24: private static long counter = 0L;
25: private static int flipper = 0;
26:
27: public static final String someString = "some long string should go here..................";
28:
29: public MultiThreadedTest() {
30: super ();
31: }
32:
33: public static void main(String[] args) throws CacheException {
34: new MultiThreadedTest().testMulti();
35: }
36:
37: public void testMulti() {
38: for (int i = 0; i < 32; i++) {
39: log.debug("Starting thread " + i);
40: Thread t = new Thread(new TestCache());
41: t.setName("" + i);
42: t.start();
43: }
44: }
45:
46: public class TestCache implements Runnable {
47:
48: /* (non-Javadoc)
49: * @see java.lang.Runnable#run()
50: */
51: public void run() {
52: final CacheManager cm = CacheManager.getInstance();
53: Cache c = null;
54: try {
55: c = cm.getCache();
56: } catch (CacheException e) {
57: // TODO Auto-generated catch block
58: e.printStackTrace();
59: }
60: for (int i = 0; i < 100000; i++) {
61:
62: //Alternate gets and puts
63: if (flipper == 0) {
64: flipper = 1;
65:
66: //log.debug("Read...");
67:
68: //Do a read.
69: final String value = (String) c.retrieve(new Long(
70: counter).toString());
71:
72: } else {
73: flipper = 0;
74:
75: //log.debug("Write...");
76:
77: //Do a write.
78: c.store(new Long(counter).toString(), someString);
79:
80: }
81: }
82: }
83: }
84:
85: }
|