简体   繁体   English

1ms Java定时器延迟太快了吗?

[英]Is 1ms Java timer delay too quick?

Timer timer = new Timer(true);
timer.scheduleAtFixedRate(timerTask, 0, 1);  // 1 = 1ms delay between each iteration

Each time it triggers it runs a super quick operation which essentially take no time at all: basically it takes the current milliseconds elapsed value and increments it while doing a quick lookup in a Map where the millis is the key. 每次触发它都会运行一个超快速的操作,它基本上不需要时间:基本上它需要当前毫秒经过的值,并在以毫秒为关键的Map中快速查找时递增它。

Do you think 1ms delay would be too fast? 你认为1ms的延迟会太快吗? Is this going to bog down the system? 这是否会使系统陷入困境? Are there any dangers in trying to use this super fast timer? 尝试使用这款超级快速计时器有危险吗?

Maybe. 也许。 A lot can happen in a millisecond on contemporary computers so it depends on lots of things. 在当代计算机上,很多事情都可以在一毫秒内完成,因此它取决于很多事情。 You probably should figure out the slowest rate acceptable and then pick something reasonable between 1 and that number. 您可能应该找出可接受的最慢速率,然后选择1和该数字之间的合理值。 This will execute more around 86,400,000 times a day. 这将每天执行更多约86,400,000次。 Does that make sense for what you are trying to accomplish? 这对你想要完成的事情有意义吗?

EDIT: As some of the comments to the question note, there might be a fundamental flaw in this approach if you assume that the timer will always succeed to execute at the rate you have provided. 编辑:作为对问题说明的一些评论,如果您假设计时器将始终以您提供的速率执行,则此方法可能存在根本缺陷。 You can never make this assumption regardless of the rate. 无论速率如何,你都无法做出这种假设。 It's hard to tell because there are very few details but I have a sense you should look into using queues instead of a Map. 这很难分辨,因为细节很少,但我觉得你应该考虑使用队列而不是Map。

That depends . 取决于

Ask yourself these questions: 问自己这些问题:

  • how often do I absolutely need that value to be checked? 我经常需要检查这个值吗?
  • how quick would be acceptable ? 有多快可以接受
  • how much of your systems CPU time are you willing to sacrifice on this task? 愿意为此任务牺牲多少系统CPU时间?

One ms can be long (high end gaming PC) or very, very short (older gen smartphone) and depending on your CPU architecture you'll end up stuffing one of many cores or the one and only core with calculating time differences. 一个ms可能很长(高端游戏PC)或非常非常短(老一代智能手机),并且根据您的CPU架构,您最终会填充多个核心中的一个,或者计算时间差异的唯一核心

As for your data structure: you'll probably need something like a sorted map containing the start as key and duration as one field of the value. 至于你的数据结构:你可能需要类似于排序映射的东西,其中包含start作为键,持续时间作为值的一个字段。 You'd fetch the closest key less than your time and check if the caption stored is still valid... or similar 您将获取最接近的密钥少于您的时间,并检查存储的标题是否仍然有效......或类似

"Do you think 1ms delay would be too fast?" “你认为1ms的延迟会太快吗?” - Yes, and it will not be able to start task each millisecond (especially on-loading), you can test it yourself (with small example below). - 是的,并且它无法在每毫秒(特别是在加载时)启动任务,您可以自己测试(下面的小例子)。

"Is this going to bog down the system?" “这会让系统陷入困境吗?” - actually not very much (I mean mostly CPU time). - 实际上不是很多(我的意思是主要是CPU时间)。 If you run your operation for example in a loop and will not make any pause, you will get much more load. 如果您在循环中运行操作并且不会暂停,则会获得更多负载。 Your system will spend big amount of time on context switch to start your task, officials it is inevitable overhead, but if you execute very simple task this overhead will be comparable with your useful workload. 您的系统将花费大量时间在上下文切换上开始您的任务,官员这是不可避免的开销,但如果您执行非常简单的任务,这个开销将与您的有用工作量相当。

"Are there any dangers in trying to use this super fast timer?" “尝试使用这款超级快速计时器有什么危险吗?” - as you wrote above " quick lookup in a Map where the millis is the key" you use millis as a key but you will not run each millisecond so your logic if count to be corrupted. - 正如您在上面写的“在毫秒是键的地图中快速查找”时,您使用毫秒作为键,但您不会运行每毫秒,因此您的逻辑会被计算为已损坏。

    static int inc = 0;
    public static void main(String[] args) throws InterruptedException {


        TimerTask timerTask = new TimerTask() {
            public void run() {                
                inc++;
            }
        };

        Timer timer = new Timer(true);
        timer.scheduleAtFixedRate(timerTask, 0, 1);        
        Thread.sleep(500000);
        timer.cancel();
        timer.purge();
        System.out.println(inc);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM