当我收到 KeepAlive 时,我想重新启动我的计时器,问题是有时它不会重新启动,而是创建一个新的,所以,最后计时器达到了限制:
public class KeepAliveTimer {
long macAddress;
Timer timer;
String ip;
public KeepAliveTimer(long mac, String ipAddress){
this.macAddress = mac;
this.ip = ipAddress;
timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
timerFinished();
}
};
timer.schedule(timerTask, 10*60*1000);
}
public void update() {
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
timerFinished();
}
};
timer.cancel();
timer.purge();
timer = new Timer();
timer.schedule(timerTask, 10*60*1000);
}
public void timerFinished() {
//tasks
}
}
对象 KeepAliveTimer 在收到第一个 keepAlive 时创建并由后续更新