本文介绍了正在视频下载(X)的准确,在Android的时钟使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的主类一个单独的线程运行。它需要发送一个消息每100毫秒,但究竟每100ms。我想知道它是否足够精确用作这样,如果还存在另一种有东西究竟发生10次。

 类ClockRun实现Runnable {    @覆盖
    公共无效的run(){
        双hourlyRate = Double.parseDouble(prefs.getString(hourlyRate,));
        双elapsedTime = 0;
        做{
            而(clockRun){
                双量= hourlyRate / 360/100 * elapsedTime;
                elapsedTime ++;
                捆绑clockData =新包();
                clockData.putString(价值,的String.format($%2F,金额));                消息消息= Message.obtain();
                message.setData(clockData);
                handler.sendMessage(消息);                尝试{
                    视频下载(100);
                }赶上(InterruptedException的E){
                    e.printStackTrace();
                }
            }
        }而(mainRun);        Notify.viaToast(getApplication(),线程停止了。);    }
}


解决方案

没有这是不准确可言,

从文档:

(暂停
      执行)指定的毫秒数,受制于precision和系统计时器和调度程序精度。

换句话说,这种睡眠是操作系统或与环境相关的,你不能predict操作系统调度决策,再加上,睡眠可以通过中断终止。

再从文档:

此外,它是没有效率的,因为你会浪费CPU周期只是用来睡觉。

将提供更好的precision和性能。
只是换你任务可赎回或Runnable接口,并使用 ScheduledExecutorService的来执行它们,有很多的教程在那里的。

I have a separate thread running in my main class. It needs to send a message every 100 milliseconds, but EXACTLY every 100ms. I am wondering if it is accurate enough to be used as such, or if there is an alternative to have something happen exactly 10 times a second.

class ClockRun implements Runnable {

    @Override
    public void run() {
        double hourlyRate = Double.parseDouble(prefs.getString("hourlyRate", ""));
        double elapsedTime = 0;
        do {
            while(clockRun){
                double amount = hourlyRate / 360 /100 * elapsedTime;
                elapsedTime++;
                Bundle clockData = new Bundle();
                clockData.putString("value", String.format("$%.2f", amount));

                Message message = Message.obtain();
                message.setData(clockData);


                handler.sendMessage(message);

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }while (mainRun);

        Notify.viaToast(getApplication(), "Thread stopped.");

    }
}
解决方案

No it is not accurate at all,

From the docs:

(temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.In other words, this sleep is OS or environment dependant, you can't predict the OS scheduling decision, plus, the sleep can be terminated by an interrupt.

Again from the docs:

In any case, you cannot assume that invoking sleep will suspend the thread for precisely the time period specified

Moreover, it is not efficient because you will be wasting CPU cycles just for sleeping.

ScheduledExecutorService will provide a better precision and performance.Simply wrap your task in Callable or Runnable and use ScheduledExecutorService to execute them, there are plenty of tutorials out there.

这篇关于正在视频下载(X)的准确,在Android的时钟使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 03:12