Consider the following scenario in Java:
- A developer has written a loop that waits for some state to be changed from a different thread.
- The loop doesn't seem to see the change.
- The developer adds a
System.out.println
statement to try to understand what is going wrong. - Now it works.
- If the print statement is removed, it stops working again.
The explanation for the behavior is that access to the shared variable is not synchronized, so the JVM is permitted to assume the value doesn't change during the loop. Adding a print statement avoids the problem because it synchronizes on the output stream. Strictly speaking, both threads must synchronize on the same object for visibility to be guaranteed, but in practice, JVMs and CPUs aren't [yet] that strict, so the print statement generally makes the code work. (The correct fix is to use proper synchronization, such as with a synchronized
block, volatile
variable, or higher-level concurrency classes such as a BlockingQueue
or SwingWorker
. Or often the user should be using asynchronous event handling instead of a busywait loop.)
Anyway, because the behavior is subtle and surprising to anyone beginning multithreaded programming, this question comes up a lot. These are the ones I've found, but there may be more:
- Boolean not correctly updating unless you print it
- Code doesn't work unless I include a println or breakpoint
- Code not executed without a print statement
- Code not working unless a simple print statement is inserted in while loop
- Commenting out a System.out.println() breaks the program?
- Function only gets triggered when a console print is in front of it, Java
- Heisenbug: Thread doesn't run without a sysout
- if statement not executing in while(true) loop
- If statement only entering if print before
- if statement only executes if I print to terminal before
- IF statement only executes if it's preceded with a 'sysout' of the value it is testing against
- In Java 7 64-bit my program freezes on a loop
- Infinite loop not seeing updated variable
- Infinite loop problem with while loop and threading
- Java code needs a system.out.println statement to run
- Java does not follow code
- Java doesn't break a while when runs in Linux
- Java: If statement in infinite while loop
- Java while loop can not detect change by thread
- Java: While loop does not exit
- Java while loop not exiting without println
- Java while loop subtlety
- Java - While loop with embedded threads bug
- Java while loop works differently on different OS
- Java static fields not working with inheritance
- Java Thread working correctly only when using println in run method
- Loop doesn't execute inner if statement
- Main thread stops in an infinite loop
- Problems in thread with unlimited loop
- Processing statement error in loop
- Program will not execute unless using System.out.println()
- Queue not working with thread?
- Server Application: Only works with a println output
- static volatile boolean - thread not getting terminated
- Strange java behavior with while loop and queue
- Thread doesn't run if i modify the run code
- Thread, while loop and a System.out.print statement
- Thread not giving correct output
- Thread only running correctly if there is a System.out.println() inside the while true loop
- Thread works when else statement is present, but when it's absent, thread stops working
- Threads: Busy Waiting - Empty While-Loop
- Very Strange Thread error with Java
- Value does not update in while loop unless printed out
- Weird Java problem, while loop termination
- Weird output with/without System.out.println() in while loop
- While loop and checking static variable
- While loop in thread seems to not be running
- While loop not ending when flag changed in different thread
- What can force a non-volatile variable to be refreshed?
- Why is an object sometimes null, when it gets initialized in a thread and accessed from main?
The answers are not always correct, and they are generally not as comprehensive as they could be. It would be nice if there was a quality, canonical Q&A of which we could mark the other and future questions as dupes. However, I can't find an ideal one, and I don't at all know how to proceed.