您的位置:首页 > 其它

TimerTask is scheduled already 异常

2014-04-04 13:34 435 查看
在使用 Timer ,TimerTask类时,当使用Timer类方法mTimer.schedule(TimerTaskLock, 1000);,将同一个TimerTaskLock设置了两次是



[java]
view plaincopy

private Timer timer = new Timer("alertTimer",true);
public void reScheduleTimer(int duration) {
timer.cancel();
timer.schedule(timerTask, 1000L, duration * 1000L);
}

,会报TimerTask is scheduled already 异常。每一个Timer会单独开启一个线程。

解决思路:It's your timerTask that is the problem. Try recreating the timerTask when you reschedule the timer."

private Timer timer = new Timer("alertTimer",true);

public void reScheduleTimer(int duration) {

timer.cancel();



timer.schedule(new TimerTask(), 1000L, duration * 1000L);//一可以使用同一Timer对象,但不能执行同一个TimerTask两次

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐