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

ThreadPool Class Reference

List of all members.

Detailed Description

Represents a group of threads of which only a set number may run at one time.

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()

Constructor & Destructor Documentation

ThreadPool  ) 
 

Create ThreadPool with default parameters

Definition at line 24 of file ThreadPool.java.

00024 {}

ThreadPool String  n,
int  max,
long  inter
 

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     }


Member Function Documentation

void add Thread  t  ) 
 

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     }

void halt  ) 
 

Signal the ThreadPool to stop.

Definition at line 84 of file ThreadPool.java.

References ThreadPool.running.

00084                        {
00085     running = false;
00086     }

SuppressWarnings "unchecked"   )  [package]
 

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     }


Member Data Documentation

long interval = 10000 [protected]
 

Interval (in ms) to wait between checks

Definition at line 15 of file ThreadPool.java.

Referenced by ThreadPool.SuppressWarnings(), and ThreadPool.ThreadPool().

int maxThreads = 4 [protected]
 

Maximum number of threads to run at once

Definition at line 13 of file ThreadPool.java.

Referenced by ThreadPool.SuppressWarnings(), and ThreadPool.ThreadPool().

String name = "anonymous" [protected]
 

Name of ThreadPool

Definition at line 11 of file ThreadPool.java.

Referenced by ThreadPool.add(), ThreadPool.SuppressWarnings(), and ThreadPool.ThreadPool().

volatile boolean running [protected]
 

Definition at line 20 of file ThreadPool.java.

Referenced by ThreadPool.halt(), and ThreadPool.SuppressWarnings().

LinkedList<Thread> runningThreads = new LinkedList<Thread>() [protected]
 

Definition at line 18 of file ThreadPool.java.

Referenced by ThreadPool.SuppressWarnings().

LinkedList<Thread> waitingThreads = new LinkedList<Thread>() [protected]
 

Definition at line 17 of file ThreadPool.java.

Referenced by ThreadPool.add(), and ThreadPool.SuppressWarnings().


The documentation for this class was generated from the following file:

Generated Wed Jan 17 09:14:27 GMT 2007