|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--it.pixel.util.threads.ThreadPool
This class is the only class you want to use directly. It is the pool itself. Use this way
RunnableWithParams rwp = new RunnableWithParams() { public void run(ThreadPool tp, Object params) { // your worker thread code here } }; ThreadPool tp = new ThreadPool(rwp); tp.execute(new SomeClassOnlyYouKnowWhichOne()); tp.execute(getSomeOtherParameter()); tp.execute(getNextThingToDoFromTheFIFO()); tp.execute("Hello world!"); // and so on...
Nested Class Summary | |
private class |
ThreadPool.PooledThread
This class extends Thread and is the actual Runnable object. |
Field Summary | |
private Semaphore |
freeThreads
Counts the number of inactive threads |
protected int |
increment
The number of threads added to the end of the freelist when the number of free threads goes below the minimum |
protected int |
max
The maximum number of threads over which no more can be created |
protected int |
maxFree
The maximum number of free threads allowed |
protected int |
min
The minimum number of threads under which no more can be discarded |
protected int |
minFree
The minimum number of free threads allowed |
private java.util.LinkedList |
params
The list of parameters to pass in FCFS order to the threads |
static int |
POOL_INCREMENT
Default number of threads added to the end of the freelist when the number of free threads goes below the minimum |
static int |
POOL_MAX
Default maximum pool size |
static int |
POOL_MAX_FREE
Default maximum number of free threads allowed |
static int |
POOL_MIN
Default minimum pool size |
static int |
POOL_MIN_FREE
Default minimum number of free threads allowed |
private RunnableWithParams |
rwp
The code to be executed by the threads of the pool. |
private boolean |
shutdown
A flag that tells the sizeManager when it is time to stop |
private java.lang.Thread |
sizeManager
A worker thread used by this class to adjust the freelist size. |
private Semaphore |
totalThreads
Keeps the total number of threads |
Constructor Summary | |
ThreadPool(RunnableWithParams rwp)
Creates the pool and initializes the limits with the repective default values. |
|
ThreadPool(RunnableWithParams rwp,
int min,
int max,
int min_free,
int max_free,
int increment)
Creates the pool and initializes the limits with the specified values. |
Method Summary | |
private void |
add(int nThreads)
Called when the pool needs to grow |
protected java.lang.Object |
drawParams()
Retrieves the first object on the worklist, and deletes it from the worklist. |
void |
ensureIncrement(int increment)
First this method tries to set the value of increment alone by calling setLimits. |
void |
ensureMax(int max)
First this method tries to set the value of max alone by calling setLimits. |
void |
ensureMaxFree(int maxFree)
First this method tries to set the value of maxFree alone by calling setLimits. |
void |
ensureMin(int min)
First this method tries to set the value of min alone by calling setLimits. |
void |
ensureMinFree(int minFree)
First this method tries to set the value of minFree alone by calling setLimits. |
void |
execute(java.lang.Object p)
Allocates a free thread from the freelist and starts the thread. |
int |
getIncrement()
Gets the value of increment |
int |
getMax()
Gets the value of max |
int |
getMaxFree()
Gets the value of maxFree |
int |
getMin()
Gets the value of min |
int |
getMinFree()
Gets the value of minFree |
RunnableWithParams |
getRunnableWithParams()
This method returns the RunnableWithParams instance passed to the constructor of this class. |
private void |
managePoolSize()
This is the method executed by a worker thread that anynchronously increases the number of free threads in the pool as needed. |
private void |
queueParams(java.lang.Object p)
Adds the specified object to the end of the worklist |
void |
setLimits(int min,
int max,
int min_free,
int max_free,
int increment)
This method sets the limits for the number of threads in the pool. |
void |
shutdown()
It tells the sizeManager it's time to stop, and waits for it to die. |
boolean |
timeToExit()
Tells whether the shutdown method has already been called or not. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int POOL_MIN_FREE
public static final int POOL_MAX_FREE
public static final int POOL_INCREMENT
public static final int POOL_MIN
public static final int POOL_MAX
protected int minFree
protected int maxFree
protected int min
protected int max
protected int increment
private Semaphore freeThreads
private Semaphore totalThreads
private java.util.LinkedList params
private java.lang.Thread sizeManager
private boolean shutdown
private RunnableWithParams rwp
Constructor Detail |
public ThreadPool(RunnableWithParams rwp)
rwp
- The RunnableWithParams called by each thread.public ThreadPool(RunnableWithParams rwp, int min, int max, int min_free, int max_free, int increment)
rwp
- The RunnableWithParams called by each thread.min
- Initial value for minmax
- Initial value for maxmin_free
- Initial value for minFreemax_free
- Initial value for maxFreeincrement
- Initial value for incrementMethod Detail |
private final void managePoolSize()
private final void add(int nThreads)
nThreads
- the number of threads to add to the freelistpublic void execute(java.lang.Object p)
p
- The arguments to pass to the thread.RunnableWithParams
public void shutdown() throws java.lang.InterruptedException
java.lang.InterruptedException
timeToExit()
private void queueParams(java.lang.Object p)
p
- The object to addpublic boolean timeToExit()
shutdown()
protected final java.lang.Object drawParams()
execute(Object)
,
RunnableWithParams
public final void setLimits(int min, int max, int min_free, int max_free, int increment)
min
- The minimum total number of threads in the poolmax
- The maximum total number of threads in the poolmin_free
- The minimum number of free threads in the poolmax_free
- The maximum number of free threads in the poolincrement
- The number of threads to add when neededmin
,
max
,
minFree
,
maxFree
,
increment
public final int getMin()
min
public final int getMax()
max
public final int getMinFree()
minFree
public final int getMaxFree()
maxFree
public final int getIncrement()
increment
public final void ensureMin(int min)
min
- The minimum total number of threads in the poolmin
,
setLimits(int, int, int, int, int)
public final void ensureMax(int max)
max
- The maximum total number of threads in the poolmax
,
setLimits(int, int, int, int, int)
public final void ensureMinFree(int minFree)
minFree
- The minimum number of free threads in the poolminFree
,
setLimits(int, int, int, int, int)
public final void ensureMaxFree(int maxFree)
maxFree
- The maximum number of free threads in the poolmaxFree
,
setLimits(int, int, int, int, int)
public final void ensureIncrement(int increment)
increment
- The number of threads to add when neededincrement
,
setLimits(int, int, int, int, int)
public RunnableWithParams getRunnableWithParams()
RunnableWithParams
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |