01: // $Id: ThreadPoolTest.java,v 1.2 2004/03/30 06:47:34 belaban Exp $
02:
03: package org.jgroups.tests;
04:
05: import org.jgroups.util.ReusableThread;
06: import org.jgroups.util.ThreadPool;
07: import org.jgroups.util.Util;
08:
09: public class ThreadPoolTest {
10:
11: static class MyThread extends Thread {
12: int num = 0;
13:
14: public MyThread(int num) {
15: this .num = num;
16: }
17:
18: public void run() {
19: long sleep_time = (long) (Math.random() * 1000);
20: //System.out.print("Thread #" + num + ": sleeping " + sleep_time + ":");
21: Util.sleep(sleep_time);
22: //System.out.println(" -- done");
23: }
24: }
25:
26: public static void main(String[] args) {
27: ThreadPool pool = new ThreadPool(5);
28: ReusableThread t;
29: MyThread my = new MyThread(1);
30: int i = 0;
31:
32: while (true) {
33: t = pool.getThread();
34: my.num = i++;
35:
36: if (t != null) {
37: System.out.println("Assigning task");
38: t.assignTask(my);
39: Util.sleep(100);
40: } else {
41: System.out
42: .println("Waiting a bit for threads to become available...");
43: Util.sleep(1000);
44: }
45:
46: }
47: }
48:
49: }
|