I am running the following program in Java (through Eclipse IDE):
package threaddemo;
class Runner extends Thread{
@Override
public void run() {
while(true)
System.out.println("Running in a thread ....");
}
}
public class ThreadClass {
public static void main(String[] args) {
Runner thread1 = new Runner();
thread1.start();
Runner thread2 = new Runner();
thread2.start();
}
}
While the program runs, I am trying to see the thread activity in JVisualVM. I was expecting to see both the threads in green concurrently (i.e. running concurrently), however I see that at any point in time during execution, for a little while any one thread is in the Monitor state, while the other is being executed. After a little while, they switch (the former is executing, while the latter is in Monitor state). How can this be?