Thread 1: is executing this loop
while(running) {
// Do Task()
}
println(\"Done\");
Thread 2 sets running false In case running is a vol
I know this is too late to add to the well explained answer. But I hope that some one will get help from my answer.The question was about when does java/jvm thread cache refresh happens?.
There is no way jvm will flush or refresh the local thread cache when thread is in runnable state. But in cases like when thread change its state by itself or external thread scheduler, then definitely local thread cache will flush to main memory. Here I just tweaked the example given from the answer
(new Thread() {
public void run() {
while (keepRunning) {
LockSupport.parkUntil(500);
}
}
}).start();
System.out.println(keepRunning);
Thread.sleep(1000);
keepRunning = false;
System.out.println(keepRunning);
Now here I just put the current into waiting thread. This method used by many java built in thread pool executor like common thread pool. So here thread will readfrom main memory and stop itself.