public class ThreadPool extends java.lang.Object implements IThreadPool
Modifier and Type | Class and Description |
---|---|
class |
ThreadPool.ServiceThread
A service thread executes tasks.
|
Modifier and Type | Field and Description |
---|---|
protected static java.util.Map<java.lang.Class<?>,java.lang.Integer> |
calls
Service calls per runnable class.
|
protected boolean |
daemon
The daemon flag.
|
protected java.util.Map<java.lang.Runnable,java.lang.Long> |
enqueuetimes
The current throughput of the pool (average of waiting times).
|
protected boolean |
finished
Boolean if already finished.
|
protected java.lang.ThreadGroup |
group
The thread group.
|
protected java.util.List<IChangeListener<java.lang.Void>> |
listeners
The finished listeners.
|
protected int |
maxparked
The maximum number of parked threads.
|
protected long |
maxwait
The time a task should maximum wait.
|
protected java.util.List<ThreadPool.ServiceThread> |
parked
The list of threads not used.
|
protected java.util.List<ThreadPool.ServiceThread> |
pool
The pool of service threads.
|
protected static int |
poolcnt
The static thread pool number.
|
static long |
PRINT_DELAY
Print every 10 seconds.
|
static boolean |
PROFILING
Enable call profiling.
|
protected boolean |
running
The running flag.
|
protected IPoolStrategy |
strategy
The strategy.
|
protected IBlockingQueue<java.lang.Runnable> |
tasks
The tasks to execute.
|
protected static int |
threadcnt
The thread number.
|
protected java.util.Timer |
timer
Rescue timer that checks if progress is made and tasks are scheduled.
|
static java.util.Map<java.lang.Thread,Future<?>> |
WAITING_THREADS
Threads waiting due to thread suspendable.
|
Constructor and Description |
---|
ThreadPool()
Create a new thread pool.
|
ThreadPool(boolean daemon,
IPoolStrategy strategy)
Create a new thread pool.
|
ThreadPool(boolean daemon,
IPoolStrategy strategy,
long maxwait)
Create a new thread pool.
|
ThreadPool(IPoolStrategy strategy)
Create a new thread pool.
|
Modifier and Type | Method and Description |
---|---|
void |
addFinishListener(IChangeListener<java.lang.Void> listener)
Add a finish listener;
|
protected void |
addThread() |
protected void |
addThreads(int num)
Create some pool.
|
void |
dispose()
Shutdown the task pool
|
void |
execute(java.lang.Runnable task)
Execute a task in its own thread.
|
protected java.lang.Runnable |
getTask(java.lang.Thread thread)
The task for a given thread.
|
boolean |
isRunning()
Test if the thread pool is running.
|
static void |
main(java.lang.String[] args)
Main for testing.
|
protected void |
notifyFinishListeners()
Notify the finish listeners.
|
java.lang.String |
toString()
Get the string representation.
|
public static final java.util.Map<java.lang.Thread,Future<?>> WAITING_THREADS
public static final boolean PROFILING
public static final long PRINT_DELAY
protected static java.util.Map<java.lang.Class<?>,java.lang.Integer> calls
protected static int threadcnt
protected static int poolcnt
protected java.lang.ThreadGroup group
protected IPoolStrategy strategy
protected java.util.List<ThreadPool.ServiceThread> pool
protected java.util.List<ThreadPool.ServiceThread> parked
protected IBlockingQueue<java.lang.Runnable> tasks
protected volatile boolean running
protected boolean daemon
protected java.util.Map<java.lang.Runnable,java.lang.Long> enqueuetimes
protected int maxparked
protected java.util.Timer timer
protected long maxwait
protected java.util.List<IChangeListener<java.lang.Void>> listeners
protected boolean finished
public ThreadPool()
public ThreadPool(IPoolStrategy strategy)
public ThreadPool(boolean daemon, IPoolStrategy strategy)
public ThreadPool(boolean daemon, IPoolStrategy strategy, long maxwait)
public boolean isRunning()
isRunning
in interface IThreadPool
public void execute(java.lang.Runnable task)
execute
in interface IThreadPool
task
- The task to execute.public void dispose()
dispose
in interface IThreadPool
public java.lang.String toString()
toString
in class java.lang.Object
protected void addThreads(int num)
num
- The number of pool.protected void addThread()
protected java.lang.Runnable getTask(java.lang.Thread thread)
public void addFinishListener(IChangeListener<java.lang.Void> listener)
addFinishListener
in interface IThreadPool
protected void notifyFinishListeners()
public static void main(java.lang.String[] args)
args
- The arguments.