Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 ALL


  Why can System.out.println be used to exit while loop

Let's first take a look at one simple Java thread code snippet which is supposed to exit the while loop after the first loop run.public class StopThread { private static boolean stopRequested; public static void main(String[] args) throws InterruptedException { Thread backgroundThread = new Thread(new Runnable() { @Override public void run() { int i = 0; while (!stopRequested) { i++; } } }); backgroundThread.start(); TimeUnit.SECONDS.sleep(1); stopRequested = true; }}But the truth is that the loop may not exit and keep running. The reason is that the updated stopRequested value will be in the ...

2,335 0       JAVA THREAD VOLATILE