Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 1ms Java timer delay too quick?

Tags:

java

timer

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?

like image 706
Brian T Hannan Avatar asked Mar 14 '23 17:03

Brian T Hannan


1 Answers

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.

like image 136
JimmyJames Avatar answered Mar 24 '23 21:03

JimmyJames