01: // $Id: SchedulerTest.java,v 1.5.20.1 2007/03/08 10:23:26 belaban Exp $
02:
03: package org.jgroups.tests;
04:
05: import junit.framework.TestCase;
06: import org.jgroups.util.Scheduler;
07: import org.jgroups.util.SchedulerListener;
08: import org.jgroups.util.ReusableThread;
09: import org.jgroups.util.Util;
10:
11: public class SchedulerTest extends TestCase {
12:
13: static class MyThread implements Runnable {
14: String name;
15:
16: MyThread(String name) {
17: this .name = name;
18: }
19:
20: public void run() {
21: long sleep_time = (long) (Math.random() * 1000);
22: System.out.println("\n--> " + name + ": sleeping for "
23: + sleep_time + " ms");
24: Util.sleep(sleep_time);
25: System.out.println("--> " + name + ": Done");
26: }
27:
28: public String toString() {
29: return "MyThread [name=" + name + ']';
30: }
31:
32: }
33:
34: static class Listener implements SchedulerListener {
35: public void started(ReusableThread rt, Runnable t) {
36: System.out.println("--> Started: " + t);
37: }
38:
39: public void stopped(ReusableThread rt, Runnable t) {
40: System.out.println("--> Stopped: " + t);
41: }
42:
43: public void suspended(ReusableThread rt, Runnable t) {
44: System.out.println("--> Suspended: " + t);
45: }
46:
47: public void resumed(ReusableThread rt, Runnable t) {
48: System.out.println("--> Resumed: " + t);
49: }
50: }
51:
52: public SchedulerTest(String name) {
53: super (name);
54: }
55:
56: public void testScheduler() throws Exception {
57: Scheduler sch = new Scheduler();
58: sch.setListener(new Listener());
59: sch.add(new MyThread("Bela"));
60: sch.add(new MyThread("Janet"));
61: sch.add(new MyThread("Ralph"));
62: sch.start();
63: sch.add(new MyThread("Frank"));
64: sch.add(new MyThread("Marco"));
65:
66: Util.sleep(1000);
67: sch.addPrio(new MyThread("Gabi"));
68: sch.add(new MyThread("Rolf"));
69: Util.sleep(100);
70: sch.addPrio(new MyThread("Gabi2"));
71: Util.sleep(100);
72: sch.addPrio(new MyThread("Gabi3"));
73: Util.sleep(100);
74: sch.addPrio(new MyThread("Gabi4"));
75: Util.sleep(1000); // increase this if you want to see all thread running (and possibly completing)
76: sch.stop();
77: }
78:
79: public static void main(String[] args) {
80: String[] testCaseName = { SchedulerTest.class.getName() };
81: junit.textui.TestRunner.main(testCaseName);
82: }
83:
84: }
|