java.lang.IllegalStateException:已经安排了TimerTask:在Android中合理使用Timer和TimerTask [英] java.lang.IllegalStateException: TimerTask is scheduled already: Rationally using of Timer and TimerTask in Android

查看:306
本文介绍了java.lang.IllegalStateException:已经安排了TimerTask:在Android中合理使用Timer和TimerTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个连接到服务器的应用程序,并向他发送 ping 命令,并用 pong 命令向服务器发送答复。



<我要实现连接超时机制。我认为它将是这样:




  • 客户端发送 ping 并使用timertask和delay启动计时器

  • 当客户端收到 pong 时,timertask被取消。



我也想要优化内存。因此,不要在每次发送 ping 命令时重新创建TimerTask。我尝试下面的代码:

  private final Timer mSystemLogoutTimer = new Timer(); 
私人最终TimerTask mLogoutTask = new TimerTask(){

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

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

私人无效onPong(){
mLogoutTask.cancel();
}

但是尝试第二次安排TimerTask时出现以下错误:

  java.lang.IllegalStateException:TimerTask已在java.util.Timer.scheduleImpl(Timer.java:572)中安排了

在java.util.Timer.schedule(Timer.java:459)

我不不明白,因为我在TimerTask上调用了 cancel()



请告诉我我做错了什么。
谢谢您的回答!

解决方案

TimerTask.cancel()不一定阻止任务的执行。根据 SDK文档,它返回 true 实际上阻止执行时,否则 false



看起来像这样吗第一次返回 true 时代码发生错误,但第二次返回时,导致 IllegalStateException 抛出紧接着调用 Timer.schedule()



您应检查 TimerTask.cancel( )的返回代码,并在返回 false 时重新创建您的 TimerTask : code> TimerTask 被烧毁,并且在该阶段无法重复使用。


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!

解决方案

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.

这篇关于java.lang.IllegalStateException:已经安排了TimerTask:在Android中合理使用Timer和TimerTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆