001: // $Id: ReusableThreadTest.java,v 1.6 2005/01/16 01:04:34 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import junit.framework.TestCase;
006: import org.jgroups.util.ReusableThread;
007: import org.jgroups.util.Util;
008:
009: public class ReusableThreadTest extends TestCase {
010:
011: public ReusableThreadTest(String name) {
012: super (name);
013: }
014:
015: static class MyThread implements Runnable {
016: int num = 0;
017: String name = null;
018:
019: public MyThread(int num) {
020: this .num = num;
021: this .name = "Thread #" + num;
022: }
023:
024: public void run() {
025:
026: System.out.println("Thread " + name + " is run");
027:
028: long sleep_time = (long) (Math.random() * 5000);
029: System.out.print("Thread #" + num + ": sleeping "
030: + sleep_time + ':');
031: Util.sleep(sleep_time);
032: //throw new Error("hello world");
033: System.out.println(" -- done");
034: }
035: }
036:
037: static class LongRunningThread extends MyThread {
038: long sleep_time = 10000;
039:
040: public LongRunningThread(int num) {
041: super (num);
042: name = "LongRunningThread #" + num;
043: }
044:
045: public LongRunningThread(int num, long sleep_time) {
046: super (num);
047: this .sleep_time = sleep_time;
048: name = "LongRunningThread #" + num;
049: }
050:
051: public void run() {
052: System.out.println("LongRunningThread " + name + " is run");
053: System.out.println("LongRunningThread #" + num
054: + ": sleeping " + sleep_time + " msecs");
055: Util.sleep(sleep_time);
056: }
057: }
058:
059: public void testReusableThread() {
060: ReusableThread t = new ReusableThread("Demo ReusableThread");
061: MyThread m1 = new MyThread(1);
062: MyThread m2 = new MyThread(2);
063:
064: LongRunningThread m4 = new LongRunningThread(4);
065:
066: System.out.println("Assigning task");
067: t.assignTask(m4);
068:
069: System.out.println("Sleeping 2 secs");
070: Util.sleep(2000);
071:
072: System.out.println("stop()");
073: t.stop();
074: System.out.println("stop() -- done");
075:
076: Util.printThreads();
077:
078: System.out.println("\nAssigning task 1");
079: t.assignTask(m1);
080: t.waitUntilDone(); // passive wait
081: System.out.println("done with task 1");
082:
083: Util.printThreads();
084:
085: System.out.println("\nAssigning task 2");
086: t.assignTask(m2);
087: t.waitUntilDone();
088: System.out.println("done with task 2");
089:
090: System.out.println("Stopping thread");
091: t.stop();
092: System.out.println("done");
093:
094: Util.printThreads();
095: }
096:
097: public void testAssignMultipleTimes() {
098: ReusableThread t = new ReusableThread("Demo ReusableThread");
099:
100: LongRunningThread t1, t2;
101: t1 = new LongRunningThread(1, 500);
102: t2 = new LongRunningThread(2, 300);
103:
104: t.start();
105:
106: t.assignTask(t1);
107: t.waitUntilDone();
108: assertTrue(t.done());
109: t.assignTask(t2);
110: t.waitUntilDone();
111: assertTrue(t.done());
112: }
113:
114: public void testStop() {
115: ReusableThread t = new ReusableThread("Demo ReusableThread");
116:
117: LongRunningThread t1;
118: t1 = new LongRunningThread(1, 20000);
119:
120: t.assignTask(t1);
121: Util.sleep(1000);
122: t.stop();
123: t.waitUntilDone();
124: assertTrue(t.done());
125: assertFalse(t.isAlive());
126: }
127:
128: public static void main(String[] args) {
129: String[] testCaseName = { ReusableThreadTest.class.getName() };
130: junit.textui.TestRunner.main(testCaseName);
131: }
132: }
|