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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With