public final class ThreadManager
extends java.lang.Object
Thread
because here the number of thread is control.ThreadedTask
, ThreadedSimpleTask
or ThreadedVerySimpleTask
, then you can do the
task as soon as possible with doThread(ThreadedTask, Object)
, do action in delayed future with
delayedThread(ThreadedTask, Object, long)
or repeat the task with
repeatThread(ThreadedTask, Object, long, long)
Modifier and Type | Class and Description |
---|---|
(package private) class |
ThreadManager.DoManagement
Dedicated thread for do the management
|
Modifier and Type | Field and Description |
---|---|
private boolean |
alive
Indicates if the manager is alive
|
private ThreadManager.DoManagement |
doManagement
Do the management
|
private boolean |
haveToActAsSoonAsPossible
Indicates if their at least a task that wait urgently for a free thread
|
(package private) static java.lang.Object |
LOCK
For synchronize the waiting task and the running threads
|
(package private) static int |
MAXIMUM_WAIT
Maximum time to wait a new free thread/task to do , before check.
|
(package private) static int |
MINIMUM_REPEAT
Minimum time between repeat (Never put under 1)
|
(package private) static int |
MINIMUM_WAIT
Minimum time before do an action (The meaning of "as soon as possible") (never put under 1)
|
private static int |
NUMBER_OF_ACTIVE_THREAD
Number of maximum thread running in same time (If you change this number, I advice to not go under 8, and if you choose a
high value, you may notice a slow or out of memory.
|
(package private) java.util.PriorityQueue<ThreadElement<?,?,?>> |
priorityQueue
Task to do waiting queue
|
static ThreadManager |
THREAD_MANAGER
Thread manager unique singleton instance
|
private ThreadedSimpleTask<java.lang.Integer> |
threadCancelTask
Delayed cancel task
|
(package private) ThreadActor[] |
threads
Threads that does the tasks
|
private int |
waitCount
Count the number of waiting process
|
Modifier | Constructor and Description |
---|---|
private |
ThreadManager()
Create a new instance of ThreadManager
|
Modifier and Type | Method and Description |
---|---|
(package private) void |
anActorIsFree()
Call by a thread to say he is free, so ready for do next task
|
int |
approximativeNumberOfFreeThread()
Give the approximative value of number of free thread.
|
void |
cancelTask(int id)
Cancel a task.
|
<PARAMETER,RESULT,PROGRESS> |
delayedThread(ThreadedTask<PARAMETER,RESULT,PROGRESS> threadedTask,
PARAMETER parameter,
long delay)
Do a task in a delayed time.
|
void |
destroy()
Destroy properly the manager.
|
<PARAMETER,RESULT,PROGRESS> |
doThread(ThreadedTask<PARAMETER,RESULT,PROGRESS> threadedTask,
PARAMETER parameter)
Do a task as soon as possible.
|
(package private) void |
internalAdd(ThreadElement<?,?,?> threadElement)
Adding a task internally.
|
boolean |
nearFull()
Indicates if thread manager is near full.
|
<PARAMETER,RESULT,PROGRESS> |
repeatThread(ThreadedTask<PARAMETER,RESULT,PROGRESS> threadedTask,
PARAMETER parameter,
long delay,
long repeat)
Repeat a task
|
void |
restart()
Restart the thread manager
|
(package private) void |
runDoManagement()
Do the management
|
private static final int NUMBER_OF_ACTIVE_THREAD
static final java.lang.Object LOCK
static final int MAXIMUM_WAIT
static final int MINIMUM_REPEAT
static final int MINIMUM_WAIT
public static final ThreadManager THREAD_MANAGER
private boolean alive
private final ThreadManager.DoManagement doManagement
private boolean haveToActAsSoonAsPossible
private final ThreadedSimpleTask<java.lang.Integer> threadCancelTask
private int waitCount
final java.util.PriorityQueue<ThreadElement<?,?,?>> priorityQueue
final ThreadActor[] threads
void anActorIsFree()
void internalAdd(ThreadElement<?,?,?> threadElement)
threadElement
- Thread to addvoid runDoManagement()
public int approximativeNumberOfFreeThread()
public void cancelTask(int id)
id
- Task id to cancel, give by repeatThread(ThreadedTask, Object, long, long)
public <PARAMETER,RESULT,PROGRESS> void delayedThread(ThreadedTask<PARAMETER,RESULT,PROGRESS> threadedTask, PARAMETER parameter, long delay)
PARAMETER
- Parameter typeRESULT
- Result type of the taskPROGRESS
- Progression information type of the taskthreadedTask
- Task to doparameter
- Parameter to give to the task, when its turn comesdelay
- Number of milliseconds to wait before do the taskpublic void destroy()
public <PARAMETER,RESULT,PROGRESS> void doThread(ThreadedTask<PARAMETER,RESULT,PROGRESS> threadedTask, PARAMETER parameter)
PARAMETER
- Parameter tpyeRESULT
- Result typePROGRESS
- Progression typethreadedTask
- Task to doparameter
- Parameter gives to the task when its turn comespublic boolean nearFull()
approximativeNumberOfFreeThread()
it can't be total accurate, but it give a good ideatrue
if thread manager is near fullpublic <PARAMETER,RESULT,PROGRESS> int repeatThread(ThreadedTask<PARAMETER,RESULT,PROGRESS> threadedTask, PARAMETER parameter, long delay, long repeat)
PARAMETER
- Parameter typeRESULT
- Result typePROGRESS
- Progress typethreadedTask
- Task to repeatparameter
- Parameter give to the task at each repeatdelay
- Time to wait in millisecond before do the first timerepeat
- Time between each repeat in millisecondscancelTask(int)
public void restart()