Package edu.jas.util
Class ThreadPool
- java.lang.Object
-
- edu.jas.util.ThreadPool
-
public class ThreadPool extends java.lang.ObjectThread pool using stack / list workpile.
-
-
Field Summary
Fields Modifier and Type Field Description private static booleandebug(package private) static intDEFAULT_SIZEDefault number of threads to use.protected intidleworkersNumber of idle workers.protected java.util.LinkedList<java.lang.Runnable>jobstackWork queue / stack.private static org.apache.logging.log4j.Loggerloggerprotected booleanshutdownShutdown request.(package private) intsizeNumber of threads to use.protected StrategyEnumerationstrategyprotected 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
-
DEFAULT_SIZE
static final int DEFAULT_SIZE
Default number of threads to use.- See Also:
- Constant Field Values
-
size
final int size
Number of threads to use.
-
workers
protected 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
-
logger
private static final org.apache.logging.log4j.Logger logger
-
debug
private static final boolean debug
-
-
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.InterruptedExceptionget 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.
-
-