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