title: Time elapsed since last data event
body: <p>I have a class (shown below) which receives data updates. My aim is to find out if no data updates have been received 5 minutes since the last data update.</p>
  
  <p>This is my code:</p>
  
  <pre><code>public class DataListener {
  
      private final ScheduledThreadPoolExecutor executor;
      private ScheduledFuture&lt;Void&gt; future;
  
      public DataUpdateListener() {
          executor = new ScheduledThreadPoolExecutor(1);
      }
  
      /**
       * Start listening for data.
       */
      public void start() {
          // do some setup...
  
          rescheduleTask();
      }
  
      /**
       * Called whenever a data event is received.
       */
      public void onEvent(final DataEvent event) {
          System.out.println("Received event: " + event);
          rescheduleTask();
      }
  
      private synchronized void rescheduleTask() {
          // cancel the existing task
          if (future != null) {
              future.cancel(true);
              executor.purge();
          }
  
          // schedule a new one
          future = executor.schedule(new Callable&lt;Void&gt;() {
              @Override
              public Void call() {
                  System.out.println("No data events received in the last 5 minutes!");
                  // do some stuff...
                  shutdown();
                  return null;
              }
          }, 5, TimeUnit.MINUTES);
      }
  
      void shutdown() {
          executor.shutdownNow();
      }    
  }
  </code></pre>
  
  <p>The class maintains a <code>ScheduledThreadPoolExecutor</code>. Whenever a data update is received, it cancels the existing task and schedules a new one. </p>
  
  <p>I'm wondering if this approach is ok or if there is a better way to do this.</p>