visualvm showing 100% cpu for sleeping threads

38 views Asked by At

I am learning the ropes on visualvm. I wrote the following small test program for playing with visualvm

    public static void main(String[] args) throws InterruptedException {
        System.out.println("hello");
        while (true) {
            Thread.sleep(1000);
            long now = System.currentTimeMillis();
            while(true) {
                long x = System.currentTimeMillis();
                if((x-now)/1000 > 1) {
                    break;
                }
            }
        }
    }

It alternatively sleeps for 1sec and pegs one core for 2 seconds. I ran it on intellij, which instruments it with some sort of agent

/usr/lib/jvm/java-11-openjdk-amd64/bin/java \
-javaagent:/home/sharath/softwares/idea-IU-232.9921.47/lib/idea_rt.jar=38335:/home/sharath/softwares/idea-IU-232.9921.47/bin \
-Dfile.encoding=UTF-8 -classpath /home/sharath/code/flinktest/target/classes sha.App

I then sampled the threads on visualVM. (Screenshot attached)visualvm screenshot

The part on the main thread makes sense - around 2/3 the time spent on cpu (94,618 ms) and rest of it sleeping (47,081ms). But what is bothering is the java.io.BufferedReader.readline(). Presumably it is the intellij java agent waiting for some user input, but it shows the thread at 100% on-cpu (141,700 ms) I'm pretty sure thats wrong, because i would have observed 1 core usage on top. But the usage was ~50% as expected. Why does visualvm show the readline() function as on-cpu?

and similarly in the threads tab, Threads tab of visual vm There are many threads like RMI threads which are shown as green (100% running) whereas i'm sure they will be sleeping most of the time. Am i missing anything?

0

There are 0 answers