线程优先级和线程精度 [英] Thread priority and Thread accuracy

查看:105
本文介绍了线程优先级和线程精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

线程优先级会提高Thread.sleep(50);的准确性吗?

Will Thread priority increases accuracy of Thread.sleep(50);?

我们知道,当您调用sleep持续50ms时,线程是不准确的,但是它是否以任何方式提高了准确性?如果线程列为MAX_PRIORITY.

As we know Threads aren't accurate when you call sleep for 50ms, But does it increases accuracy by any mean? If thread is listed as MAX_PRIORITY.

将感谢您的任何解释.

推荐答案

睡眠的准确性取决于操作系统.如果要提高准确性,可以使用其他操作系统.另一种方法是不睡觉,可以忙着等待.或者,您可以睡45毫秒,忙于等待5毫秒.

The accuracy of sleep is down to the operating system. If you want greater accuracy you can use another OS. Another approach is don't sleep, you can busy wait instead. Or you can sleep for say 45 ms and busy wait for 5 ms.

如果您有一个任务需要每秒运行20次,那么最好跟踪下一次应在何时运行该任务并在该时间(而不是等待固定的时间)运行该任务的时间两次睡眠之间也需要一些时间.

If you have a task which you need to run 20 times per second, you are better off keeping track of when the tasks should run next and run it at the time (rather than wait a fixed amount of time) as what you do between sleeps will also take some time.

BTW这是

BTW This is what ScheduledExecutorService.scheduleAtFixedRate does for you.

它可以在任务之间以微秒级的精度休眠(假设操作系统支持该任务),但它不会漂移.

It can sleep between task with micro-second accuracy (assuming the OS supports it) but it tries not to drift.

这篇关于线程优先级和线程精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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