qflib 0.98.0

de.qfs.lib.util
Class DelayedAction

java.lang.Object
  |
  +--de.qfs.lib.util.DelayedAction

public class DelayedAction
extends java.lang.Object

A DelayedAction is something that is generally useful in event handling, if subsequent events of the same kind cancel each other and the number of pending events is unknown at the time one event is handled.

In such a case, handling only the last event can be a big improvement in performance.

The DelayedAction works by starting a timer that will execute a Runnable after a certain delay. If another event is received, before the timer has fired, the pending action is dropped and the timer is restarted for the new action.

An example application for this pattern is a SelectionListener for a Component, that updates the display of another Component whenever the selection of the first Component changes. If the display of the second Component is complex, e.g. a large table, there will be an annoying delay if e.g. the user uses the arrow keys to navigate through a list or tree component and the display is updated for every element on the way. A slight delay of a few hundred milliseconds (300 is a good value) after the target element has been reached will go almost unnoticed though.

Another example use is in a daemon thread that listens for some kind of output from other threads and acts on it. It is often a lot more efficient to collect a few elements from whatever source in a buffer, instead of handling each as soon as it arrives.

Warning: As a side effect of using a Timer, execution of the Runnable is happening inside the AWT event loop thread. This is an advantage if any Swing methods are called, but it means that the actions should either not take too long, so as not to affect GUI reactivity, or delegate the task to yet another thread.

Version:
$Revision: 1.4 $
Author:
Gregor Schmid

Constructor Summary
DelayedAction(int delay)
          Create a new DelayedAction.
 
Method Summary
 void cancel()
          Cancel a pending action.
 boolean isActionPending()
          Query the DelayedAction whether an action is currently pending.
 void perform(java.lang.Runnable runnable)
          Schedule an action to be performed after the delay.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DelayedAction

public DelayedAction(int delay)
Create a new DelayedAction.
Parameters:
delay - The delay in milliseconds until an action is performed.
Method Detail

perform

public void perform(java.lang.Runnable runnable)
Schedule an action to be performed after the delay. This cancels any pending actions for the DelayedAction.
Parameters:
runnable - The Runnable to execute after the delay.

cancel

public void cancel()
Cancel a pending action.

isActionPending

public boolean isActionPending()
Query the DelayedAction whether an action is currently pending.
Returns:
True if an action is pending.

qflib 0.98.0