So, I start the Java server, and in one class I have a static timer. Then I have another class that has many instances that are created and destroyed throughout the entire program life cycle, each with its own TimerTask (using a static timer). When an instance is destroyed, it calls cancel () in TimerTask.
The problem is that I'm not sure if this is a good design, and sometimes errors occur when an instance creates and configures TimerTask:
java.lang.IllegalStateException: Timer already cancelled.
Here is some code to show what I mean.
private void initTasks()
{
roomHeartbeatTask = new RoomHeartbeatTask(this);
RoomListExtension.roomHeartbeat.schedule(roomHeartbeatTask, 0, 1000);
}
private void cancelTasks()
{
roomHeartbeatTask.cancel();
}
source
share