我正在使用计时器更新图片,但它会导致致命的性能问题。然后我打印日志并发现我的计时器运行得更快而不是 1500。 那有什么问题呢?
以错误的速度安排:
startToDraw().353: System.currentTimeMillis() = 1332742387400
startToDraw().353: System.currentTimeMillis() = 1332742387410
startToDraw().353: System.currentTimeMillis() = 1332742387438
startToDraw().353: System.currentTimeMillis() = 1332742387449
startToDraw().353: System.currentTimeMillis() = 1332742387472
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
while (!isDestroyed) {
try {
Log.i(getClass().getName(), "startToDraw().353: System.currentTimeMillis() = " + System.currentTimeMillis());
handler.sendMessage(new Message());
} catch (Exception e) {
}
}
}
}, 0, 1500);
最佳答案
我认为问题在于您正在进入一个重复运行的 while 循环。您可能只想检查自己是否处于无效状态,如果处于无效状态,则不要执行任务。所以我会改变:
while (!isDestroyed) {
try {
Log.i(getClass().getName(), "startToDraw().353: System.currentTimeMillis() = " + System.currentTimeMillis());
handler.sendMessage(new Message());
} catch (Exception e) {
}
到:
if (!isDestroyed) {
try {
Log.i(getClass().getName(), "startToDraw().353: System.currentTimeMillis() = " + System.currentTimeMillis());
handler.sendMessage(new Message());
} catch (Exception e) {
}
关于android - 我的定时器线程有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9867575/