Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

ThreadPool.java

Go to the documentation of this file.
00001 package cedar.jetweb.util;
00002 
00003 import java.util.LinkedList;
00004 import java.util.Iterator;
00005 
00009 public class ThreadPool extends Thread {
00011     protected String name = "anonymous";
00013     protected int maxThreads = 4;
00015     protected long interval = 10000;
00016 
00017     protected LinkedList<Thread> waitingThreads = new LinkedList<Thread>();
00018     protected LinkedList<Thread> runningThreads = new LinkedList<Thread>();
00019 
00020     protected volatile boolean running;
00021 
00022 
00024     public ThreadPool() {}
00025 
00027     public ThreadPool(String n, int max, long inter) {
00028     name = n;
00029     maxThreads = max;
00030     interval = inter;
00031     System.out.println("Creating Thread Pool \""+name+"\" with max threads = "+maxThreads+
00032                " and interval "+interval+" ms");
00033     }
00034 
00038     public void add(Thread t) {
00039     System.out.println("ThreadPool "+name+": adding thread");
00040     waitingThreads.add(t);
00041     System.out.println(" "+waitingThreads.size()+" threads now waiting");
00042     }
00043 
00045     @SuppressWarnings("unchecked")
00046     public void run() {
00047     running = true;
00048     while (running) {
00049         // Remove threads that have finished.
00050         LinkedList<Thread> tempRunningThreads = (LinkedList<Thread>)runningThreads.clone();
00051         Iterator<Thread> iter = runningThreads.iterator();
00052         while (iter.hasNext()) {
00053         Thread t = (Thread) iter.next();
00054         if (!t.isAlive()) tempRunningThreads.remove(t); // ??? Safe?
00055         }
00056         runningThreads = tempRunningThreads;
00057 
00058         // Add threads from waitingThreads to runningThreads and start them,
00059         // until all slots filled.
00060         iter = waitingThreads.iterator();
00061         tempRunningThreads = (LinkedList<Thread>)runningThreads.clone();
00062         LinkedList<Thread> tempWaitingThreads = (LinkedList<Thread>) waitingThreads.clone();
00063         while (iter.hasNext() && maxThreads-tempRunningThreads.size() > 0) {
00064         Thread t = iter.next();
00065         tempWaitingThreads.remove(t);
00066         tempRunningThreads.add(t);
00067         t.start();
00068         System.out.println("ThreadPool "+name+" starting a thread, "+
00069                    tempWaitingThreads.size()+" still waiting.");
00070         }
00071         runningThreads = tempRunningThreads;
00072         waitingThreads = tempWaitingThreads;
00073 
00074         // Sleep for interval ms before checking again.
00075         try {
00076         this.sleep(interval);
00077         } catch (InterruptedException e) {}
00078     }
00079     }
00080 
00084     public void halt() {
00085     running = false;
00086     }
00087 
00088 }

Generated Wed Jan 17 09:14:27 GMT 2007