|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object mavis.util.LastExecutor
public class LastExecutor
Executes the last Runnable tasks of a series of tasks after a delay. The worker thread will terminate automatically when there are no runnables to be executed. Optionally executes all of the tasks and not only the last one. All operations are thread-safe.
This class can be used for example in connection with a "continuous search" invoked by a series of GUI events (such as a DocumentListener), but it is necessary to react to only the last event after a short period of user inactivity.
Nested Class Summary | |
---|---|
private class |
LastExecutor.LastExecutorThread
Keeps on checking the LastExecutor.queue to see if there are Runnables to be executed. |
private class |
LastExecutor.RunDelayed
Wraps a Runnable object and sets the delay after which it should be executed by a worker thread. |
Field Summary | |
---|---|
private int |
delayMillis
Defines how long is the delay in milliseconds, after which the events need to be run. |
private boolean |
execOnlyLast
Defines if only the last event should be executed. |
private java.util.concurrent.DelayQueue<LastExecutor.RunDelayed> |
queue
Prioritized FIFO queue for containing the RunDelayed items that have not expired. |
private java.lang.Thread |
workerThread
The worker thread that will run the inserted runnables. |
Constructor Summary | |
---|---|
LastExecutor()
Creates an empty LastExecutor with a delay of 0 and execOnlyLast set to true. |
|
LastExecutor(boolean execOnlyLast)
Creates an empty LastExecutor with a delay of 0. |
|
LastExecutor(int delayMillis)
Creates an empty LastExecutor with execOnlyLast set to true. |
|
LastExecutor(int delayMillis,
boolean execOnlyLast)
Creates an empty LastExecutor. |
Method Summary | |
---|---|
void |
clear()
Removes all of the elements from the execution queue. |
void |
execute(java.lang.Runnable command)
Inserts a runnable task to the end of the queue. |
int |
getDelayMillis()
|
boolean |
isExecOnlyLast()
|
void |
join()
Waits for the queue to become empty. |
static void |
main(java.lang.String[] args)
TEST METHOD |
void |
setDelayMillis(int delayMillis)
|
void |
setExecOnlyLast(boolean execOnlyLast)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private int delayMillis
private boolean execOnlyLast
private java.util.concurrent.DelayQueue<LastExecutor.RunDelayed> queue
private java.lang.Thread workerThread
Constructor Detail |
---|
public LastExecutor()
public LastExecutor(int delayMillis)
delayMillis
- the length of execution delay in milliseconds; if less than 0, then 0 will be used.public LastExecutor(boolean execOnlyLast)
execOnlyLast
- if true, only the last event will be executed after the delay; otherwise all are executed in
order of appearance.public LastExecutor(int delayMillis, boolean execOnlyLast)
delayMillis
- the length of execution delay in milliseconds; if less than 0, then 0 will be used.execOnlyLast
- if true, only the last task will be executed after the delay; otherwise all are executed in
order of appearance.Method Detail |
---|
public boolean isExecOnlyLast()
public void setExecOnlyLast(boolean execOnlyLast)
execOnlyLast
- if true, only the last task will be executed after the delay; otherwise all are executed in
order of appearance.public int getDelayMillis()
public void setDelayMillis(int delayMillis)
delayMillis
- delay in milliseconds; if less than 0, then the new value is ignored.public void execute(java.lang.Runnable command)
execute
in interface java.util.concurrent.Executor
command
- the runnable task to be executed after a pre-defined delay.
java.lang.NullPointerException
- if command is null.public void join() throws java.lang.InterruptedException
java.lang.InterruptedException
- if another thread has interrupted the current thread. The interrupted status of the
current thread is cleared when this exception is thrown.public void clear()
public static void main(java.lang.String[] args) throws java.lang.InterruptedException
java.lang.InterruptedException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |