|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.ThreadGroup
mandala.util.ThreadPool
Threads pool implementation.
A threads pool usually prevent the non-negligeable cost of threads creation using a pool of already pre-allocated threads.
The behaviour of an instance of this class is defined by many parameters.
min
: the minimum amount of threads this pool
must contain;max
: the maximum amount of threads this pool
may contain;reactivity
: the timeout of the
priority
the priority of
the threads in the pool;daemonStatus
: the daemon status of
the threads in the pool.This pool has a max
limits. If this instance is given more tasks than it
contains threads, the max
limit, to run the given number
of tasks. To prevent a n
at a time. The
reactivity
to
balance the pool size related to the tasks size.
WARNING : You really must take care of code which insert Runnable task which insert others Runnable task and wait for their termination: deadlocks may occurs !
ThreadGroup
Nested Class Summary | |
class |
ThreadPool.ThreadPoolCancelable
|
Field Summary | |
protected boolean |
daemonStatus
Daemon status given to |
static boolean |
DEFAULT_DAEMON_STATUS
Deamon status given to new threads in the pool. |
static int |
DEFAULT_MAX_THREADS_NB
Default maximum of threads in a ThreadPool . |
static java.lang.String |
DEFAULT_MAX_THREADS_NB_PROPERTY
|
static int |
DEFAULT_MIN_THREADS_NB
Default minimum of threads in a ThreadPool . |
static java.lang.String |
DEFAULT_MIN_THREADS_NB_PROPERTY
|
static int |
DEFAULT_PRIORITY
Priority given to new threads in the pool. |
static java.lang.String |
DEFAULT_PRIORITY_PROPERTY
|
static long |
DEFAULT_REACTIVITY_TIMER
Time in milliseconds of the |
static java.lang.String |
DEFAULT_REACTIVITY_TIMER_PROPERTY
|
static Syslog |
DEFAULT_SYSLOG
The Syslog used for logging when none are specified
using the setSyslog() method. |
static int |
DELTA_PRIORITY
Default priority is set to Thread.NORM_PRIORITY +
DELTA_PRIORITY . |
protected static int |
instancesNumber
Number of ThreadPool instances used for automatic name
generation in ThreadPool(int min, int max) . |
protected int |
max
Maximum amount of pre-allocated threads in this thread pool. |
protected int |
min
Minimum amount of pre-allocated threads in this thread pool. |
protected int |
priority
Priority given to |
protected long |
reactivity
Reactivity of the |
protected Syslog |
syslog
The Syslog object facility. |
Constructor Summary | |
ThreadPool()
Creates a new ThreadPool instance. |
|
ThreadPool(int min,
int max)
Creates a new ThreadPool instance. |
|
ThreadPool(java.lang.String name)
Creates a new ThreadPool instance. |
|
ThreadPool(java.lang.String name,
int min,
int max)
Creates a new ThreadPool instance. |
|
ThreadPool(java.lang.ThreadGroup parent,
java.lang.String name,
int min,
int max)
Creates a new ThreadPool instance. |
|
ThreadPool(java.lang.ThreadGroup parent,
java.lang.String name,
int min,
int max,
int priority,
boolean daemonStatus,
long reactivity)
Creates a new ThreadPool instance. |
Method Summary | |
void |
cancel()
Cancel the execution of all tasks in this threadpool. |
boolean |
cancel(ThreadPool.ThreadPoolCancelable cancelable)
Cancel the execution of a previously given task. |
boolean[] |
cancel(ThreadPool.ThreadPoolCancelable[] cancelables)
Cancel the execution of previously given tasks. |
void |
delete()
Destroy this threadpool and all of its related threads. |
ThreadPool.ThreadPoolCancelable |
execute(java.lang.Runnable task)
Request the execution of the specified task . |
protected void |
finalize()
|
boolean |
getDaemonStatus()
Get the daemon status given to |
int |
getMax()
Get the maximum amount of pre-allocated threads in this thread pool. |
int |
getMin()
Get the minimum amount of pre-allocated threads in this thread pool. |
int |
getPriority()
Get the priority given to |
long |
getReactivity()
Get the reactivity of the |
Syslog |
getSyslog()
Returns the Syslog instance used for logging. |
boolean |
interrupt(ThreadPool.ThreadPoolCancelable cancelable)
Interrupt the execution of a task currently executed by a thread of this thread pool. |
boolean[] |
interrupt(ThreadPool.ThreadPoolCancelable[] cancelables)
Interrupt the execution of tasks currently executed by some threads of this thread pool. |
boolean |
isStarted()
Returns true if this thread pool has been started,
false otherwize. |
void |
setSyslog(Syslog syslog)
Set the Syslog instance to use for logging. |
void |
start()
Starts the ThreadPool. |
java.lang.String |
toString()
|
Methods inherited from class java.lang.ThreadGroup |
activeCount, activeGroupCount, allowThreadSuspension, checkAccess, destroy, enumerate, enumerate, enumerate, enumerate, getMaxPriority, getName, getParent, interrupt, isDaemon, isDestroyed, list, parentOf, resume, setDaemon, setMaxPriority, stop, suspend, uncaughtException |
Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected static int instancesNumber
Number of ThreadPool
instances used for automatic name
generation in ThreadPool(int min, int max)
.
public static Syslog DEFAULT_SYSLOG
The Syslog
used for logging when none are specified
using the setSyslog()
method.
Syslog
,
setSyslog(Syslog)
protected Syslog syslog
The Syslog
object facility.
Each message (debug, log, warning, error, ...) are wrote to this message logger.
Default value is set to DEFAULT_SYSLOG
.
Syslog
public static final int DEFAULT_MIN_THREADS_NB
Default minimum of threads in a ThreadPool
.
The value of this field can be customized by the
mandala.util.ThreadPool.min
property. Default is new
Double(Math.ceil(Runtime.availableProcessors() *
1.5)).intValue()
.
public static final java.lang.String DEFAULT_MIN_THREADS_NB_PROPERTY
public static final int DEFAULT_MAX_THREADS_NB
Default maximum of threads in a ThreadPool
.
The value of this field can be customized by the
mandala.util.ThreadPool.max
property. Default is
Integer.MAX_VALUE
.
public static final java.lang.String DEFAULT_MAX_THREADS_NB_PROPERTY
public static final int DELTA_PRIORITY
Default priority is set to Thread.NORM_PRIORITY +
DELTA_PRIORITY
.
public static final int DEFAULT_PRIORITY
Priority given to new threads in the pool.
The value of this field can be customized by the
mandala.util.ThreadPool.priority
property. Default is
Thread.NORM_PRIORITY + DELTA_PRIORITY
.
public static final java.lang.String DEFAULT_PRIORITY_PROPERTY
public static final boolean DEFAULT_DAEMON_STATUS
Deamon status given to new threads in the pool.
Default is always true
.
public static final long DEFAULT_REACTIVITY_TIMER
Time in milliseconds of the
The value of this field can be customized by the
mandala.util.ThreadPool.reactivity
property. Default is
5 seconds.
public static final java.lang.String DEFAULT_REACTIVITY_TIMER_PROPERTY
protected int min
Minimum amount of pre-allocated threads in this thread pool.
protected int max
Maximum amount of pre-allocated threads in this thread pool.
protected int priority
Priority given to
protected boolean daemonStatus
Daemon status given to
protected long reactivity
Reactivity of the
Constructor Detail |
public ThreadPool(java.lang.String name)
Creates a new ThreadPool
instance.
Equivalent to ThreadPool(name, DEFAULT_MIN_THREADS_NB,
DEFAULT_MAX_THREADS_NB)
.
name
- the name of the new thread pool.public ThreadPool()
Creates a new ThreadPool
instance.
Equivalent to ThreadPool(DEFAULT_MIN_THREADS_NB,
DEFAULT_MAX_THREADS_NB)
.
public ThreadPool(int min, int max)
Creates a new ThreadPool
instance.
Equivalent to ThreadPool(
. where gname
is where gname is a newly
generated name. Automatically generated names are of the form
ThreadPool.class.getName() + "#" + n
, where n
is an integer.
public ThreadPool(java.lang.String name, int min, int max)
Creates a new ThreadPool
instance.
Equivalent to
ThreadPool(Thread.currentThread().getThreadGroup(), name, min,
max)
.
public ThreadPool(java.lang.ThreadGroup parent, java.lang.String name, int min, int max)
Creates a new ThreadPool
instance.
Equivalent to ThreadPool(parent, name, min, max,
DEFAULT_PRIORITY, DEFAULT_DAEMON_STATUS,
DEFAULT_REACTIVITY_TIMER)
.
public ThreadPool(java.lang.ThreadGroup parent, java.lang.String name, int min, int max, int priority, boolean daemonStatus, long reactivity)
Creates a new ThreadPool
instance.
The created instance has parent
as parent's thread
group, name
as name and an initial pool of min
threads. It may allocate a maximum of max
threads if
needed. The threads in the pool will be assigned to the given
priority
and daemonStatus
. The
reactivity
parent
- the parent thread group.name
- the name of the new thread pool.min
- the minimum amount of pre-allocated threads in this thread
pool.max
- the maximum amount of pre-allocated threads in this thread
pool.priority
- the priority given to daemonStatus
- the daemon status given to reactivity
- the reactivity given to the
Method Detail |
public Syslog getSyslog()
Returns the Syslog
instance used for logging.
Syslog
instance used for logging.public void setSyslog(Syslog syslog)
Set the Syslog
instance to use for logging.
syslog
- the Syslog
instance to use for logging.Syslog
public int getMin()
Get the minimum amount of pre-allocated threads in this thread pool.
public int getMax()
Get the maximum amount of pre-allocated threads in this thread pool.
public int getPriority()
Get the priority given to
public boolean getDaemonStatus()
Get the daemon status given to
public long getReactivity()
Get the reactivity of the
public java.lang.String toString()
public boolean isStarted()
Returns true
if this thread pool has been started,
false
otherwize.
true
if this thread pool has been started,
false
otherwize.public void start()
Starts the ThreadPool.
This method also start min
threads, sets their priority
to priority
, their daemon status to
daemonStatus
and start the thread-balancer.
After this invocation, isStarted()
always returns
true
and further call to this method does nothing.
public void delete()
Destroy this threadpool and all of its related threads.
public ThreadPool.ThreadPoolCancelable execute(java.lang.Runnable task)
Request the execution of the specified task
.
task
- the Runnable
to execute in a separate thread.
public boolean cancel(ThreadPool.ThreadPoolCancelable cancelable)
Cancel the execution of a previously given task.
This method does nothing if the task related to the specified
cancelable
has already been executed, or if it is currently
executed by a thread of the pool.
cancelable
- the ThreadPoolCancelable
instance
returned by execute()
.
true
if the given task has been succesfully
cancelled, false
otherwise.execute(Runnable task)
public boolean[] cancel(ThreadPool.ThreadPoolCancelable[] cancelables)
Cancel the execution of previously given tasks.
cancelables
- the Cancelable
instances returned by
execute()
.
i
is the value that may have been returned by
cancel(cancelables[i])
.cancel(ThreadPool.ThreadPoolCancelable cancelable)
public void cancel()
Cancel the execution of all tasks in this threadpool.
When this method returns, any previous tasks given for execution has been canceled. Note however some tasks may still be executed by threads in this pool.
public boolean interrupt(ThreadPool.ThreadPoolCancelable cancelable)
Interrupt the execution of a task currently executed by a thread of this thread pool.
This method does nothing if the task related to the specified
cancelable
has already been executed, or if it is not yet
executed by a thread of this thread pool.
cancelable
- the Cancelable
instance returned by
execute()
.
true
if the given task has been succesfully
interrupted, false
otherwise.execute(Runnable task)
public boolean[] interrupt(ThreadPool.ThreadPoolCancelable[] cancelables)
Interrupt the execution of tasks currently executed by some threads of this thread pool.
cancelables
- the Cancelable
instances returned by
execute()
.
i
is the value that may have been returned by
interrupt(cancelables[i])
.interrupt(ThreadPool.ThreadPoolCancelable cancelable)
protected void finalize() throws java.lang.Throwable
java.lang.Throwable
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |