mavis.util
Class LastExecutor

java.lang.Object
  extended by mavis.util.LastExecutor
All Implemented Interfaces:
java.util.concurrent.Executor

public class LastExecutor
extends java.lang.Object
implements java.util.concurrent.Executor

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

delayMillis

private int delayMillis
Defines how long is the delay in milliseconds, after which the events need to be run.


execOnlyLast

private boolean execOnlyLast
Defines if only the last event should be executed. If false, then all of the events are executed in the order of appearance.


queue

private java.util.concurrent.DelayQueue<LastExecutor.RunDelayed> queue
Prioritized FIFO queue for containing the RunDelayed items that have not expired. If execOnlyLast is true, then this queue should never contain more than one item.


workerThread

private java.lang.Thread workerThread
The worker thread that will run the inserted runnables. If the thread has no more work to do, it will set workerThread to null and terminate itself.

Constructor Detail

LastExecutor

public LastExecutor()
Creates an empty LastExecutor with a delay of 0 and execOnlyLast set to true.


LastExecutor

public LastExecutor(int delayMillis)
Creates an empty LastExecutor with execOnlyLast set to true.

Parameters:
delayMillis - the length of execution delay in milliseconds; if less than 0, then 0 will be used.

LastExecutor

public LastExecutor(boolean execOnlyLast)
Creates an empty LastExecutor with a delay of 0.

Parameters:
execOnlyLast - if true, only the last event will be executed after the delay; otherwise all are executed in order of appearance.

LastExecutor

public LastExecutor(int delayMillis,
                    boolean execOnlyLast)
Creates an empty LastExecutor.

Parameters:
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

isExecOnlyLast

public boolean isExecOnlyLast()
Returns:
true if only the last task will be executed after the delay; otherwise false.

setExecOnlyLast

public void setExecOnlyLast(boolean execOnlyLast)
Parameters:
execOnlyLast - if true, only the last task will be executed after the delay; otherwise all are executed in order of appearance.

getDelayMillis

public int getDelayMillis()
Returns:
the delay in milliseconds.

setDelayMillis

public void setDelayMillis(int delayMillis)
Parameters:
delayMillis - delay in milliseconds; if less than 0, then the new value is ignored.

execute

public void execute(java.lang.Runnable command)
Inserts a runnable task to the end of the queue. It will remain there until it is executed or another object replaces it. If execOnlyLast is set to true, the queue will be cleared before inserting this runnable to it. If there is no worker thread running, a new one will be spawned.

Specified by:
execute in interface java.util.concurrent.Executor
Parameters:
command - the runnable task to be executed after a pre-defined delay.
Throws:
java.lang.NullPointerException - if command is null.

join

public void join()
          throws java.lang.InterruptedException
Waits for the queue to become empty.

Throws:
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.

clear

public void clear()
Removes all of the elements from the execution queue. The queue will be empty after this call returns. The execution thread will stop after the currently running task, if any.


main

public static void main(java.lang.String[] args)
                 throws java.lang.InterruptedException
TEST METHOD

Throws:
java.lang.InterruptedException