gpt4 book ai didi

java - 处理程序延迟不统一?

转载 作者:行者123 更新时间:2023-11-29 07:29:13 25 4
gpt4 key购买 nike

我有这段代码运行延迟:

    final Handler handler = new Handler();
final int delay = 1000; //milliseconds
handler.postDelayed(new Runnable(){
public void run(){
handler.postDelayed(this, delay);
Log.e("LOG","HIT");
}
}, delay);

并接收这些日志语句:

07-27 19:02:00.084 1690-1690/com.example.steinbockapplications.myapplication2 E/LOG: HIT
07-27 19:02:02.089 1690-1690/com.example.steinbockapplications.myapplication2 E/LOG: HIT
07-27 19:02:03.092 1690-1690/com.example.steinbockapplications.myapplication2 E/LOG: HIT
07-27 19:02:25.154 1690-1690/com.example.steinbockapplications.myapplication2 E/LOG: HIT
07-27 19:02:26.155 1690-1690/com.example.steinbockapplications.myapplication2 E/LOG: HIT

认为它应该每 1000 毫秒运行一次,那么这些延迟是怎么回事?

最佳答案

你好@Johnny Rockex,我运行了相同的代码,稍作修改。

07-27 22:50:19.999 18973-18973/com.jingged.mermaids E/LOG: HIT
07-27 22:50:21.000 18973-18973/com.jingged.mermaids E/LOG: HIT
07-27 22:50:22.001 18973-18973/com.jingged.mermaids E/LOG: HIT
07-27 22:50:23.002 18973-18973/com.jingged.mermaids E/LOG: HIT
07-27 22:50:24.003 18973-18973/com.jingged.mermaids E/LOG: HIT
07-27 22:50:25.005 18973-18973/com.jingged.mermaids E/LOG: HIT
07-27 22:50:26.006 18973-18973/com.jingged.mermaids E/LOG: HIT
07-27 22:50:27.007 18973-18973/com.jingged.mermaids E/LOG: HIT
07-27 22:50:28.008 18973-18973/com.jingged.mermaids E/LOG: HIT
07-27 22:50:29.009 18973-18973/com.jingged.mermaids E/LOG: HIT

代码

final Handler handler = new Handler();
final int delay = 1000; //milliseconds
handler.postDelayed(new Runnable(){
public void run(){
Log.e("LOG","HIT");
handler.postDelayed(this, delay);
}
}, delay);

实际上你是在日志方法之前延迟它。这应该是运行方法下的处理程序中的最后一行

关于java - 处理程序延迟不统一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45356955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com