简体   繁体   中英

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.

Do you think 1ms delay would be too fast? 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. This will execute more around 86,400,000 times a day. 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.

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?

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.

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. 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?" - 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). 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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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