java.lang.IllegalStateException: TimerTask is scheduled already: Rationally using of Timer and TimerTask in Android

二次信任 提交于 2019-12-21 12:57:23

问题


I write an application that connects to server and sends him ping commands, server answer with pong commands.

I want to implement connection timeout mechanism. I think it will be following:

  • Client send ping and start timer with timertask and delay
  • When client receive pong, timertask is cancelled.

Also, I want to optimize memory. So, not to recreate TimerTask every time I send ping command. I try code below:

private final Timer mSystemLogoutTimer = new Timer();
private final TimerTask mLogoutTask = new TimerTask() {

    @Override
    public void run() {
        mMessageInterface.onConnectionTimeout();
        cancel();
    }
};

private void ping() {
    sendRequest(RequestBuilder.formPing());
    mSystemLogoutTimer.schedule(mLogoutTask, CoreConst.PING_ANSWER_DELAY);
}

private void onPong() {
    mLogoutTask.cancel();
}

But I get following error when try to schedule TimerTask second time:

java.lang.IllegalStateException: TimerTask is scheduled already
at java.util.Timer.scheduleImpl(Timer.java:572)
at java.util.Timer.schedule(Timer.java:459)

I don't understand, because I call cancel() on TimerTask.

Please tell me what I am doing wrong. Thank you for answers!


回答1:


TimerTask.cancel() does not necessarily prevent the execution of the task. According to the SDK documentation, it returns true when the execution was actually prevented, false otherwise.

Looks like this it what happens with your code as the first time true is returned, but not the second, leading an IllegalStateException to be thrown when calling Timer.schedule() right after.

You should check TimerTask.cancel()'s return code, and recreate your TimerTask when false is returned: the TimerTask is burnt and cannot be reused at that stage.



来源:https://stackoverflow.com/questions/23648258/java-lang-illegalstateexception-timertask-is-scheduled-already-rationally-usin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!