Definition at line 9 of file ThreadPool.java.
Public Member Functions | |
ThreadPool () | |
ThreadPool (String n, int max, long inter) | |
void | add (Thread t) |
void | halt () |
Protected Attributes | |
String | name = "anonymous" |
int | maxThreads = 4 |
long | interval = 10000 |
LinkedList< Thread > | waitingThreads = new LinkedList<Thread>() |
LinkedList< Thread > | runningThreads = new LinkedList<Thread>() |
volatile boolean | running |
Package Functions | |
SuppressWarnings ("unchecked") public void run() |
|
Create ThreadPool with default parameters Definition at line 24 of file ThreadPool.java. 00024 {}
|
|
Create ThreadPool with given parameters Definition at line 27 of file ThreadPool.java. References ThreadPool.interval, ThreadPool.maxThreads, and ThreadPool.name. 00027 { 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 }
|
|
Add a Thread to this ThreadPool. Definition at line 38 of file ThreadPool.java. References ThreadPool.name, and ThreadPool.waitingThreads. 00038 { 00039 System.out.println("ThreadPool "+name+": adding thread"); 00040 waitingThreads.add(t); 00041 System.out.println(" "+waitingThreads.size()+" threads now waiting"); 00042 }
|
|
Signal the ThreadPool to stop. Definition at line 84 of file ThreadPool.java. References ThreadPool.running. 00084 { 00085 running = false; 00086 }
|
|
Start checking for threads to run Definition at line 45 of file ThreadPool.java. References ThreadPool.interval, ThreadPool.maxThreads, ThreadPool.name, ThreadPool.running, ThreadPool.runningThreads, and ThreadPool.waitingThreads. 00046 { 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 }
|
|
Interval (in ms) to wait between checks Definition at line 15 of file ThreadPool.java. Referenced by ThreadPool.SuppressWarnings(), and ThreadPool.ThreadPool(). |
|
Maximum number of threads to run at once Definition at line 13 of file ThreadPool.java. Referenced by ThreadPool.SuppressWarnings(), and ThreadPool.ThreadPool(). |
|
Name of ThreadPool Definition at line 11 of file ThreadPool.java. Referenced by ThreadPool.add(), ThreadPool.SuppressWarnings(), and ThreadPool.ThreadPool(). |
|
Definition at line 20 of file ThreadPool.java. Referenced by ThreadPool.halt(), and ThreadPool.SuppressWarnings(). |
|
Definition at line 18 of file ThreadPool.java. Referenced by ThreadPool.SuppressWarnings(). |
|
Definition at line 17 of file ThreadPool.java. Referenced by ThreadPool.add(), and ThreadPool.SuppressWarnings(). |
Generated Wed Jan 17 09:14:27 GMT 2007