Java 静态计时器可以处理多个调用 cancel() 的 TimerTasks 吗?
所以我正在运行一个 Java 服务器,并且在一个类中我有一个静态计时器。然后我有另一个类,它在程序的整个生命周期中有许多实例被创建和销毁,每个实例都有自己的 TimerTask (使用静态 Timer)。当实例被销毁时,它会调用 TimerTask 上的 cancel() 。
问题是,我不确定这是否是一个好的设计,而且在实例创建和调度其 TimerTask 时有时也会出现错误:
java.lang.IllegalStateException: Timer already cancelled.
这里有一些代码来说明我的意思。
/**
* Starts scheduled tasks using TimerTask
*/
private void initTasks()
{
// room heartbeat thread: start immediately, run once every second
roomHeartbeatTask = new RoomHeartbeatTask(this);
RoomListExtension.roomHeartbeat.schedule(roomHeartbeatTask, 0, 1000);
// add additional tasks here
}
/**
* Cancels all tasks that have been started by this extension
*/
private void cancelTasks()
{
roomHeartbeatTask.cancel();
}
So I'm running a Java server and in one class I have a static Timer. Then I have another class which has many instances being created and destroyed throughout the life of the program, each with their own TimerTask (using the static Timer). When an instance is destroyed, it calls cancel() on the TimerTask.
The problem is, I'm not sure if this is good design, and also I get errors sometimes when the instance is creating and scheduling its TimerTask:
java.lang.IllegalStateException: Timer already cancelled.
Here is some code to show what I mean.
/**
* Starts scheduled tasks using TimerTask
*/
private void initTasks()
{
// room heartbeat thread: start immediately, run once every second
roomHeartbeatTask = new RoomHeartbeatTask(this);
RoomListExtension.roomHeartbeat.schedule(roomHeartbeatTask, 0, 1000);
// add additional tasks here
}
/**
* Cancels all tasks that have been started by this extension
*/
private void cancelTasks()
{
roomHeartbeatTask.cancel();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误是因为当您调用
cancel()
时,Timer
已“死亡”,您无法对其执行任何其他操作。来自 API :
您有几个选择:
Timer
并在每个对象中保留一个单独的Timer
实例。这样,当对象被销毁时,您也可以销毁Timer
而不会影响其他TimerTask
。这意味着每个对象有一个计时器线程。Timer
,但也在内存中(例如在ArrayList
中)保存其中所有任务的列表。然后,当一个对象被销毁时,您可以使用内存中的任务列表(减去与您销毁的对象相对应的任务列表)重新创建静态 Timer 对象。这样做的结果是,剩余的 TimerTask 的执行可能需要一些技巧(特别是如果您不希望它们全部“聚集”)。The error is because when you call
cancel()
theTimer
is 'dead' and you can't do anything else with it.From the API:
You have a few options:
Timer
and instead hold a separate instance ofTimer
within each object. That way when the object is destroyed, you can also destroy theTimer
without affecting the otherTimerTask
s. This will mean one timer thread per object.Timer
but also hold in memory (e.g. in anArrayList<TimerTask>
) a list of all the tasks within it. Then when an object is destroyed, you re-create the staticTimer
object using the in-memory list of tasks (minus the one corresponding to the object you destoryed). The consequence of this is that the execution of the remainingTimerTask
s may need some finessing (particularly if you don't want them all to 'bunch up').Timer
-like class to allow for the removal ofTimerTask
s. (Someone may have already done this, I don't know)