code: private void swapAndCancelTask(AtomicReference<TimerTask> taskRef,
                                   TimerTask newTask) {
      TimerTask oldTask = taskRef.getAndSet(newTask);
      if (oldTask != null) {
        oldTask.cancel();
      }
    }
docs: Sets the new task and attempts to cancelTask the old one.
  
  @param taskRef task reference
  @param newTask new task