java - 处理程序未在 1 秒内运行 1000 次

标签 java android handler runnable android-handler

我有一个任务,我想每毫秒更新一些内容(某种计算)。现在这就是我正在做的事情,延迟 1 毫秒。-

 int countertest =0;

 myRunnable = new Runnable() {
                    @Override
                    public void run() {
                        countertest++;
                        System.out.println("----counter----->"+countertest);
                        myHandler.postDelayed(myRunnable, 1); //runs every 1 millisecond
                    }
                };

我通过单击按钮启动 myHandler 并在 1 秒后停止它 (removeCallbacksAndMessages(null))。现在,myHandler 应该在一秒钟内运行它 1000 次。但它有时会运行 110 次,有时会运行 119 次……有时会运行一些随机数。不知道为什么它没有在 1 秒内运行 1000 次。任何人都可以详细说明一下吗?

我现在应该怎么做才能在 1 秒内执行 1000 次?举个例子,如果我想在一秒内显示从 0000 到 1000 的文本,或者在 1 秒内精确打印 0-1000,既不超过 1 秒也不少于 1 秒。 反对者请。请在下面发表评论,以便我知道我错在哪里。谢谢。

用于上述测试的设备 - Samsung S9。

最佳答案

如上所述here ,根本无法保证“您的”系统的毫秒精度:

Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

关于java - 处理程序未在 1 秒内运行 1000 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51383626/

相关文章:

java - 从 GCM 迁移到 FCM

c# - 从 TCP Socket 接收和发送连续数据

Android自定义对话框按钮XML onClick错误

javascript - 如何为将 gmail 作为默认邮件处理程序的用户在新选项卡中打开 mailto 链接?

java - 从 session 监听器访问和修改应用程序范围的托管 Bean 的属性

java - 无法更改 getView() 中的线性布局高度

java - 单击按钮时更改位置?

java - 无法连接到 ec2 实例上的 Tomcat 服务器

java - 与音频服务一起使用时 mContext 类型不匹配

android - 使用来自 Android 的 SOAP 调用 Magento api 时的 XMLPull 解析器异常