Package edu.jas.util
Class ThreadPool
- java.lang.Object
-
- edu.jas.util.ThreadPool
-
public class ThreadPool extends java.lang.Object
Thread pool using stack / list workpile.- Author:
- Akitoshi Yoshida, Heinz Kredel
-
-
Field Summary
Fields Modifier and Type Field Description protected intidleworkersNumber of idle workers.protected java.util.LinkedList<java.lang.Runnable>jobstackWork queue / stack.protected booleanshutdownShutdown request.protected StrategyEnumerationstrategyprotected edu.jas.util.PoolThread[]workersArray of workers.
-
Constructor Summary
Constructors Constructor Description ThreadPool()Constructs a new ThreadPool with strategy StrategyEnumeration.FIFO and size DEFAULT_SIZE.ThreadPool(int size)Constructs a new ThreadPool with strategy StrategyEnumeration.FIFO.ThreadPool(StrategyEnumeration strategy)Constructs a new ThreadPool with size DEFAULT_SIZE.ThreadPool(StrategyEnumeration strategy, int size)Constructs a new ThreadPool.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddJob(java.lang.Runnable job)adds a job to the workpile.intcancel()Cancels the threads.protected java.lang.RunnablegetJob()get a job for processing.intgetNumber()number of worker threads.StrategyEnumerationgetStrategy()get used strategy.booleanhasJobs()check if there are jobs for processing.booleanhasJobs(int n)check if there are more than n jobs for processing.voidinit()thread initialization and start.voidterminate()Terminates the threads.java.lang.StringtoString()toString.
-
-
-
Field Detail
-
workers
protected edu.jas.util.PoolThread[] workers
Array of workers.
-
idleworkers
protected int idleworkers
Number of idle workers.
-
shutdown
protected volatile boolean shutdown
Shutdown request.
-
jobstack
protected java.util.LinkedList<java.lang.Runnable> jobstack
Work queue / stack.
-
strategy
protected StrategyEnumeration strategy
-
-
Constructor Detail
-
ThreadPool
public ThreadPool()
Constructs a new ThreadPool with strategy StrategyEnumeration.FIFO and size DEFAULT_SIZE.
-
ThreadPool
public ThreadPool(StrategyEnumeration strategy)
Constructs a new ThreadPool with size DEFAULT_SIZE.- Parameters:
strategy- for job processing.
-
ThreadPool
public ThreadPool(int size)
Constructs a new ThreadPool with strategy StrategyEnumeration.FIFO.- Parameters:
size- of the pool.
-
ThreadPool
public ThreadPool(StrategyEnumeration strategy, int size)
Constructs a new ThreadPool.- Parameters:
strategy- for job processing.size- of the pool.
-
-
Method Detail
-
init
public void init()
thread initialization and start.
-
toString
public java.lang.String toString()
toString.- Overrides:
toStringin classjava.lang.Object
-
getNumber
public int getNumber()
number of worker threads.
-
getStrategy
public StrategyEnumeration getStrategy()
get used strategy.
-
terminate
public void terminate()
Terminates the threads.
-
cancel
public int cancel()
Cancels the threads.
-
addJob
public void addJob(java.lang.Runnable job)
adds a job to the workpile.- Parameters:
job-
-
getJob
protected java.lang.Runnable getJob() throws java.lang.InterruptedException
get a job for processing.- Throws:
java.lang.InterruptedException
-
hasJobs
public boolean hasJobs()
check if there are jobs for processing.
-
hasJobs
public boolean hasJobs(int n)
check if there are more than n jobs for processing.- Parameters:
n- Integer- Returns:
- true, if there are possibly more than n jobs.
-
-