KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: SchedulerTest.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.Scheduler;
7 import org.jgroups.util.SchedulerListener;
8 import org.jgroups.util.Util;
9
10
11 public class SchedulerTest extends TestCase {
12
13     static class MyThread implements Runnable JavaDoc {
14         String JavaDoc name;
15
16         MyThread(String JavaDoc 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 " + sleep_time + " ms");
23             Util.sleep(sleep_time);
24             System.out.println("--> " + name + ": Done");
25         }
26
27         public String JavaDoc toString() {
28             return "MyThread [name=" + name + ']';
29         }
30
31     }
32
33
34     static class Listener implements SchedulerListener {
35         public void started(Runnable JavaDoc t) {
36             System.out.println("--> Started: " + t);
37         }
38
39         public void stopped(Runnable JavaDoc t) {
40             System.out.println("--> Stopped: " + t);
41         }
42
43         public void suspended(Runnable JavaDoc t) {
44             System.out.println("--> Suspended: " + t);
45         }
46
47         public void resumed(Runnable JavaDoc t) {
48             System.out.println("--> Resumed: " + t);
49         }
50     }
51
52
53     public SchedulerTest(String JavaDoc name) {
54         super(name);
55     }
56
57
58     public void testScheduler() throws Exception JavaDoc {
59         Scheduler sch=new Scheduler();
60         sch.setListener(new Listener());
61         sch.add(new MyThread("Bela"));
62         sch.add(new MyThread("Janet"));
63         sch.add(new MyThread("Ralph"));
64         sch.start();
65         sch.add(new MyThread("Frank"));
66         sch.add(new MyThread("Marco"));
67
68         Util.sleep(1000);
69         sch.addPrio(new MyThread("Gabi"));
70         sch.add(new MyThread("Rolf"));
71         Util.sleep(100);
72         sch.addPrio(new MyThread("Gabi2"));
73         Util.sleep(100);
74         sch.addPrio(new MyThread("Gabi3"));
75         Util.sleep(100);
76         sch.addPrio(new MyThread("Gabi4"));
77         Util.sleep(1000); // increase this if you want to see all thread running (and possibly completing)
78
sch.stop();
79     }
80
81     public static void main(String JavaDoc[] args) {
82         String JavaDoc[] testCaseName={SchedulerTest.class.getName()};
83         junit.textui.TestRunner.main(testCaseName);
84     }
85
86 }
87
Popular Tags