1 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 { 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 " + sleep_time + " ms"); 23 Util.sleep(sleep_time); 24 System.out.println("--> " + name + ": Done"); 25 } 26 27 public String toString() { 28 return "MyThread [name=" + name + ']'; 29 } 30 31 } 32 33 34 static class Listener implements SchedulerListener { 35 public void started(Runnable t) { 36 System.out.println("--> Started: " + t); 37 } 38 39 public void stopped(Runnable t) { 40 System.out.println("--> Stopped: " + t); 41 } 42 43 public void suspended(Runnable t) { 44 System.out.println("--> Suspended: " + t); 45 } 46 47 public void resumed(Runnable t) { 48 System.out.println("--> Resumed: " + t); 49 } 50 } 51 52 53 public SchedulerTest(String name) { 54 super(name); 55 } 56 57 58 public void testScheduler() throws Exception { 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); sch.stop(); 79 } 80 81 public static void main(String [] args) { 82 String [] testCaseName={SchedulerTest.class.getName()}; 83 junit.textui.TestRunner.main(testCaseName); 84 } 85 86 } 87 | Popular Tags |