1 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 { 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 Util.sleep(sleep_time); 21 } 23 } 24 25 26 27 public static void main(String [] 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 |