Is 1 ms Java timer delay too fast?

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

Each time it starts, it starts an ultrafast operation, which takes almost no time: basically it takes the current millisecond of the expired value and increases it by performing a quick search on the map where the key is the millis.

Do you think a 1 ms delay would be too fast? Will this intimidate the system? Are there any dangers when trying to use this super fast timer?

+4
source share
3 answers

It depends on .

Ask yourself the following questions:

  • How often do i need value to check?
  • How fast is acceptable ?
  • ?

( ) , ( ), ​​ .

: , , , , . , ...

+2

. , . , , , - 1 . 86 400 000 . , ?

: , , , , . . , , , .

+5

" , 1 ?" - , ( ), ( ).

" ?" - ( ). , , , . , , , , .

"Are there any dangers when trying to use this super fast timer?" - as you wrote above, “quick search on the map, where millis is the key”, you use millis as the key, but you will not start every millisecond so that your logic if the account is damaged.

    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);
    }
0
source

Source: https://habr.com/ru/post/1622764/


All Articles