I don't understand why using timer variable like this in the following codes.
Question 1: In both startTimer() and stopTimer(), there is a local variable aTimer to be used before the operation on timer, what is the intention?
Question 2: In stopTimer(), timer will be assigned null, so if timer is not null which means this timer is already created, when startTimer() is called, timer will not be created again. Is this best practice to check whether timer is running or not? By assign null to timer, PMD also reports violation on "NullAssignment"
private Timer timer;
private void startTimer() {
if (timer == null) {
Timer aTimer = timerFactory.createTimer(100000, null);
aTimer.setListener(this);
timer = aTimer;
}
}
private void stopTimer() {
if (timer != null) {
Timer aTimer = timer;
timer = null;
aTimer.cancel();
aTimer.setListener(null);
}
}
public void start() {
synchronized(..) {
startTimer();
}
}
public void stop() {
synchronized(..) {
stopTimer();
}
}