Package com.sun.jini.thread
Class TaskManager
java.lang.Object
com.sun.jini.thread.TaskManager
A task manager manages a single queue of tasks, and some number of worker threads. New tasks are
added to the tail of the queue. Each thread loops, taking a task from the queue and running it.
Each thread looks for a task by starting at the head of the queue and taking the first task (that
is not already being worked on) that is not required to run after any of the tasks that precede
it in the queue (including tasks that are currently being worked on).
This class uses the
Logger named com.sun.jini.thread.TaskManager to log information at the
following logging levels:
| Level | Description |
|---|---|
SEVERE | failure to create a worker thread when no other worker threads exist |
WARNING | exceptions thrown by TaskManager.Task methods,
and failure to create a worker thread when other worker threads exist |
- Author:
- Sun Microsystems, Inc.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThe interface that tasks must implement -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intIndex of the first pending task; all earlier tasks are activeprotected final floatThreshold for creating new threadsprotected static final org.slf4j.LoggerLoggerprotected final intMaximum number of threads allowedprotected final ListRead-only view of tasksprotected final ArrayList<TaskManager.Task>Active and pending tasksprotected booleanTrue if manager has been terminatedprotected final List<com.sun.jini.thread.TaskManager.TaskThread>Active threadsprotected final longIdle time before a thread should exit -
Constructor Summary
ConstructorsConstructorDescriptionCreate a task manager with maxThreads = 10, timeout = 15 seconds, and loadFactor = 3.0.TaskManager(int maxThreads, long timeout, float loadFactor) Create a task manager.TaskManager(int maxThreads, long timeout, float loadFactor, String threadName, int retriesOnIdle) TaskManager(int maxThreads, long timeout, float loadFactor, String threadName, int retriesOnIdle, int threadPriority) -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a new task.voidaddAll(Collection c) Add all tasks in a collection, in iterator order.voidAdd a new task if it is not equal to (using the equals method) to any existing active or pending task.intReturn the maximum number of threads to use on tasks.Return all pending tasks.intintintReturns the total number of tasks to be executed and currently executing.booleanprotected booleanReturn true if a new thread should be created (ignoring maxThreads).booleanbooleanRemove a task if it is pending (not active).voidsetThreadName(String threadName) voidInterrupt all threads, and stop processing tasks.
-
Field Details
-
logger
protected static final org.slf4j.Logger loggerLogger -
tasks
Active and pending tasks -
firstPending
protected int firstPendingIndex of the first pending task; all earlier tasks are active -
roTasks
Read-only view of tasks -
threads
Active threads -
maxThreads
protected final int maxThreadsMaximum number of threads allowed -
timeout
protected final long timeoutIdle time before a thread should exit -
loadFactor
protected final float loadFactorThreshold for creating new threads -
terminated
protected boolean terminatedTrue if manager has been terminated
-
-
Constructor Details
-
TaskManager
public TaskManager()Create a task manager with maxThreads = 10, timeout = 15 seconds, and loadFactor = 3.0. -
TaskManager
public TaskManager(int maxThreads, long timeout, float loadFactor) Create a task manager.- Parameters:
maxThreads- maximum number of threads to use on taskstimeout- idle time before a thread exitsloadFactor- threshold for creating new threads. A new thread is created if the total number of runnable tasks (both active and pending) exceeds the number of threads times the loadFactor, and the maximum number of threads has not been reached.
-
TaskManager
public TaskManager(int maxThreads, long timeout, float loadFactor, String threadName, int retriesOnIdle) -
TaskManager
public TaskManager(int maxThreads, long timeout, float loadFactor, String threadName, int retriesOnIdle, int threadPriority)
-
-
Method Details
-
addIfNew
Add a new task if it is not equal to (using the equals method) to any existing active or pending task. -
add
Add a new task. -
setThreadName
-
getThreadName
-
addAll
Add all tasks in a collection, in iterator order. -
needThread
protected boolean needThread()Return true if a new thread should be created (ignoring maxThreads). -
removeIfPending
Remove a task if it is pending (not active). Object identity (==) is used, not the equals method. Returns true if the task was removed. -
remove
-
terminate
public void terminate()Interrupt all threads, and stop processing tasks. Only getPending should be used afterwards. -
isTerminated
public boolean isTerminated() -
getPending
Return all pending tasks. A new list is returned each time. -
getTotalTasks
public int getTotalTasks()Returns the total number of tasks to be executed and currently executing. -
getMaxThreads
public int getMaxThreads()Return the maximum number of threads to use on tasks. -
getThreadCount
public int getThreadCount()- Returns:
- the number of threads currently allocated (less or equal to getMaxThreads())
- Since:
- 12.3
-
getRunningThreadCount
public int getRunningThreadCount()- Returns:
- the number of threads actively running a task
- Since:
- 12.3
-