/Users/lyon/j4p/src/classUtils/pack/util/pool/test/ObjectPoolTest.java
|
1 package classUtils.pack.util.pool.test;
2
3 import java.util.Random;
4
5 import classUtils.pack.util.pool.ObjectPool;
6
7 public class ObjectPoolTest {
8
9 ObjectPool pool;
10 Random random = new Random();
11 Thread []threads = new Thread[2];
12
13 class TestThread extends Thread {
14
15 private int tn;
16
17 TestThread(int tn) { super(""+tn); this.tn=tn; }
18
19 public void run() {
20 while(true) {
21 try {
22 System.out.println(this+" acquiring object");
23 System.out.flush();
24 Object obj = pool.acquire();
25 System.out.println(this+" - object "+obj+" acquired");
26 int sleepTime=random.nextInt(100);
27 System.out.println(this+" sleeping for "+sleepTime+"ms");
28 Thread.sleep(sleepTime);
29
30 if (random.nextBoolean()) {
31 System.out.println(this+" renewing object "+obj);
32 obj=pool.renew(obj);
33 }
34
35 System.out.println(this+" releasing object "+obj);
36 pool.release(obj);
37 System.out.println(this+" - object "+obj+" released");
38 System.out.println(pool);
39 sleepTime=random.nextInt(100);
40 System.out.println(this+" sleeping for "+sleepTime+"ms");
41 Thread.sleep(sleepTime);
42 } catch(InterruptedException e) {
43 }
44 }
45 }
46
47 }
48
49 public ObjectPoolTest() throws Exception {
50 //pool=ExtendedObjectPool.newPool(1, "java.lang.String", new Object[] { new String("foo") });
51 pool=ObjectPool.newPool(1, "java.lang.Object", new Object[] { });
52 for(int i=0;i<threads.length;i++) {
53 threads[i]=new TestThread(i);
54 }
55 }
56
57 public void run() {
58 for(int i=0;i<threads.length;i++) {
59 threads[i].start();
60 }
61 }
62
63 public static void main(String args[]) throws Exception {
64 ObjectPoolTest test = new ObjectPoolTest();
65 test.run();
66 }
67
68 }
69
70