The stream sleeps too long

I wrote a simple program and noticed that my streams are too fast to run JVisualVM, note which application I want to monitor, connect and watch streams. Then I made them sleep. And they noticed that the tools now report them as sleeping (or almost only about sleep). Although they work. This begs the question: why ?!

    new Thread(() -> {
        while(true) {
            System.out.println("not sleeping");
            try {
                Thread.sleep(0,1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }, "not sleeping!").start();

This thread should sleep for 1 nanosecond, but look how it told JVisualVM:

ran a minute but ran 5s

After starting for 57 seconds, it reports the operating time of 5s and rests as if sleeping - is it a mistake or is my understanding incorrect?

This is not just JVisualVM:

$ while true; do jstack 8277 | grep -A 2 not; done              [% 20:34:27]
"not sleeping!" #10 prio=5 os_prio=0 tid=0x00007f5ea82d1000 nid=0x2078 waiting on condition [0x00007f5e917c6000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)

jstackalso only occasionally reports this stream as RUNNING. I would expect different proportions, so please help me understand.

+4
1

, , 1 . , , , , /. , , .

, . , Java nanos .

+1

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


All Articles