KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > ReusableThreadTest


1 // $Id: ReusableThreadTest.java,v 1.1 2007/07/04 07:29:34 belaban Exp $
2

3 package org.jgroups.tests;
4
5 import junit.framework.TestCase;
6 import org.jgroups.util.ReusableThread;
7 import org.jgroups.util.Util;
8
9
10 public class ReusableThreadTest extends TestCase {
11
12
13     public ReusableThreadTest(String JavaDoc name) {
14         super(name);
15     }
16
17
18     static class MyThread implements Runnable JavaDoc {
19         int num=0;
20         String JavaDoc name=null;
21
22         public MyThread(int num) {
23             this.num=num;
24             this.name="Thread #" + num;
25         }
26
27         public void run() {
28
29             System.out.println("Thread " + name + " is run");
30
31             long sleep_time=(long)(Math.random() * 5000);
32             System.out.print("Thread #" + num + ": sleeping " + sleep_time + ':');
33             Util.sleep(sleep_time);
34             //throw new Error("hello world");
35
System.out.println(" -- done");
36         }
37     }
38
39
40     static class LongRunningThread extends MyThread {
41         long sleep_time=10000;
42
43
44         public LongRunningThread(int num) {
45             super(num);
46             name="LongRunningThread #" + num;
47         }
48
49         public LongRunningThread(int num, long sleep_time) {
50             super(num);
51             this.sleep_time=sleep_time;
52             name="LongRunningThread #" + num;
53         }
54
55         public void run() {
56             System.out.println("LongRunningThread " + name + " is run");
57             System.out.println("LongRunningThread #" + num + ": sleeping " + sleep_time + " msecs");
58             Util.sleep(sleep_time);
59         }
60     }
61
62
63
64
65     public void testReusableThread() {
66         ReusableThread t=new ReusableThread("Demo ReusableThread");
67         MyThread m1=new MyThread(1);
68         MyThread m2=new MyThread(2);
69
70         LongRunningThread m4=new LongRunningThread(4);
71
72         System.out.println("Assigning task");
73         t.assignTask(m4);
74
75         System.out.println("Sleeping 2 secs");
76         Util.sleep(2000);
77
78         System.out.println("stop()");
79         t.stop();
80         System.out.println("stop() -- done");
81
82         Util.printThreads();
83
84         System.out.println("\nAssigning task 1");
85         t.assignTask(m1);
86         t.waitUntilDone(); // passive wait
87
System.out.println("done with task 1");
88
89         Util.printThreads();
90
91         System.out.println("\nAssigning task 2");
92         t.assignTask(m2);
93         t.waitUntilDone();
94         System.out.println("done with task 2");
95
96         System.out.println("Stopping thread");
97         t.stop();
98         System.out.println("done");
99
100         Util.printThreads();
101     }
102
103
104     public void testAssignMultipleTimes() {
105         ReusableThread t=new ReusableThread("Demo ReusableThread");
106
107         LongRunningThread t1, t2;
108         t1=new LongRunningThread(1, 500);
109         t2=new LongRunningThread(2, 300);
110
111         t.start();
112
113         t.assignTask(t1);
114         t.waitUntilDone();
115         assertTrue(t.done());
116         t.assignTask(t2);
117         t.waitUntilDone();
118         assertTrue(t.done());
119     }
120
121     public void testStop() {
122         ReusableThread t=new ReusableThread("Demo ReusableThread");
123
124         LongRunningThread t1;
125         t1=new LongRunningThread(1, 20000);
126
127         t.assignTask(t1);
128         Util.sleep(1000);
129         t.stop();
130         t.waitUntilDone();
131         assertTrue(t.done());
132         assertFalse(t.isAlive());
133     }
134
135
136     public static void main(String JavaDoc[] args) {
137         String JavaDoc[] testCaseName={ReusableThreadTest.class.getName()};
138         junit.textui.TestRunner.main(testCaseName);
139     }
140 }
141
Popular Tags