KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: ThreadPoolTest.java,v 1.2 2004/03/30 06:47:34 belaban Exp $
2

3 package org.jgroups.tests;
4
5
6 import org.jgroups.util.ReusableThread;
7 import org.jgroups.util.ThreadPool;
8 import org.jgroups.util.Util;
9
10
11 public class ThreadPoolTest {
12
13     static class MyThread extends Thread JavaDoc {
14     int num=0;
15     public MyThread(int num) {this.num=num;}
16     
17     public void run() {
18         long sleep_time=(long)(Math.random() * 1000);
19         //System.out.print("Thread #" + num + ": sleeping " + sleep_time + ":");
20
Util.sleep(sleep_time);
21         //System.out.println(" -- done");
22
}
23     }
24
25     
26
27     public static void main(String JavaDoc[] args) {
28     ThreadPool pool=new ThreadPool(5);
29     ReusableThread t;
30     MyThread my=new MyThread(1);
31     int i=0;
32
33     while(true) {
34         t=pool.getThread();
35         my.num=i++;
36
37         if(t != null) {
38         System.out.println("Assigning task");
39         t.assignTask(my);
40         Util.sleep(100);
41         }
42         else {
43         System.out.println("Waiting a bit for threads to become available...");
44         Util.sleep(1000);
45         }
46
47     }
48     }
49
50
51
52 }
53
Popular Tags