01: package org.jgroups.tests;
02:
03: import junit.framework.Test;
04: import junit.framework.TestCase;
05: import junit.framework.TestSuite;
06:
07: import org.jgroups.util.Util;
08:
09: import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
10: import EDU.oswego.cs.dl.util.concurrent.ThreadFactory;
11:
12: /**
13: * Test playground for util.concurrent.PooledExecutor
14: *
15: *
16: * @author Vladimir Blagojevic
17: * @version $Id$
18: *
19: */
20:
21: public class TestPoolExecutor extends TestCase {
22: static int count;
23: private final Object poolLock = new Object();
24:
25: public void testPool() throws InterruptedException {
26: long keepAlive = 30 * 1000;
27: PooledExecutor executor = new PooledExecutor(5);
28: executor.setMinimumPoolSize(1);
29: executor.waitWhenBlocked();
30: executor.setKeepAliveTime(keepAlive);
31: executor.setThreadFactory(new ThreadFactory() {
32: public Thread newThread(final Runnable command) {
33: synchronized (poolLock) {
34: count++;
35: }
36: return new Thread("poolid=" + count) {
37:
38: public void run() {
39: System.out.println("Thread "
40: + Thread.currentThread() + " started");
41: command.run();
42: System.out.println("Thread "
43: + Thread.currentThread() + " stopped");
44: }
45: };
46: }
47: });
48:
49: for (int i = 0; i < 30; i++) {
50: final int count = i;
51: executor.execute(new Runnable() {
52: public void run() {
53: System.out
54: .println("Runnable " + count + " running");
55: //use timing here that approximates how long
56: //this thread needs to run
57: Util.sleep(3000);
58: }
59: });
60:
61: //use timing here that approximates time
62: //between tasks arriving
63: Util.sleep(1000);
64: }
65:
66: executor.shutdownAfterProcessingCurrentlyQueuedTasks();
67: //see if all threads are stop/recycled
68: Util.sleep(keepAlive);
69: }
70:
71: protected void setUp() throws Exception {
72: // TODO Auto-generated method stub
73: super .setUp();
74: }
75:
76: protected void tearDown() throws Exception {
77: // TODO Auto-generated method stub
78: super .tearDown();
79: }
80:
81: public static Test suite() {
82: return new TestSuite(TestPoolExecutor.class);
83: }
84:
85: public static void main(String[] args) {
86: String[] testCaseName = { TestPoolExecutor.class.getName() };
87: junit.textui.TestRunner.main(testCaseName);
88: }
89: }
|