Class DebounceTimer


  • public class DebounceTimer
    extends Object
    A timer that can be used to ensure that time-consuming tasks, usually triggered by events, do not run too often. It can be used by code that fires events to avoid firing too many events, or by code listening for events to avoid running the handler too often.
    • Field Detail

      • DEFAULT_DELAY_MILLISECONDS

        public static final int DEFAULT_DELAY_MILLISECONDS
        See Also:
        Constant Field Values
    • Constructor Detail

      • DebounceTimer

        public DebounceTimer()
        Creates a new DebounceTimer with the default delay. When this DebounceTimer is no longer needed the shutdown() method should be called to release resources.
      • DebounceTimer

        public DebounceTimer​(int delayMilliseconds)
        Creates a new DebounceTimer with the given delay (in milliseconds). When this DebounceTimer is no longer needed the shutdown() method should be called to release resources.
    • Method Detail

      • debounce

        public void debounce​(Runnable runnable)
        Starts a timer that will run the runnable after a short delay (on a separate thread). If another call to this method occurs before the timer expires then the timer will be reset. When there are multiple quick calls to this method and the time between each call is less than the delay then the runnable will only run when the delay is reached. This method is non-blocking.

        Code example:
         public void handleEvent(RowsSetEvent e) {
             if(e.containsColumn(CyNetwork.SELECTED)) {
                 debounceTimer.debounce(() -> updateUI());
             }
         }
         
      • debounce

        public void debounce​(Object key,
                             Runnable runnable)
        Starts a timer that will run the runnable after a short delay (on a separate thread). If another call to this method (with the same key) occurs before the timer expires then the timer will be reset. When there are multiple quick calls to this method and the time between each call is less than the delay then the runnable will only run when the delay is reached. This method is non-blocking.

        Code example:
         public void handleEvent(RowsSetEvent e) {
             if(e.containsColumn(CyNetwork.SELECTED)) {
                 CyNetworkView networkView = applicationManager.getCurrentNetworkView();
                 if(networkView != null) {
                     debounceTimer.debounce(networkView, () -> updateUI(networkView));
                 }
             }
         }
         
        Parameters:
        key - A key object to associate with the runnable, each key object will get its own timer.
        Throws:
        RejectedExecutionException - if this timer has been shutdown
        NullPointerException - if key or runnable are null
      • shutdown

        public void shutdown()
        Releases resources.
      • isShutdown

        public boolean isShutdown()