109

Consider the following scenario in Java:

  1. A developer has written a loop that waits for some state to be changed from a different thread.
  2. The loop doesn't seem to see the change.
  3. The developer adds a System.out.println statement to try to understand what is going wrong.
  4. Now it works.
  5. 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:



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.

8
  • 48
    +1 - This Meta question turned out better than I expected from reading the title.
    – Bill the Lizard Mod
    Commented Aug 19, 2014 at 13:03
  • 2
    The answer that you get on Meta for such questions is always a variation of: Invest your time to create a great resource. No point asking on Meta because the answer is always the same.
    – usr
    Commented Aug 19, 2014 at 15:09
  • 1
    There's a list of authoritative questions in the java tag wiki. You might add one yourself.
    – tbodt
    Commented Aug 19, 2014 at 15:22
  • 2
    @usr I guess I wanted to know if it was a supported idea before I went meddling. Also, although I've heard of canonical questions, I'm unsure if there's any sort of special procedure I'd need to follow in creating one.
    – Boann
    Commented Aug 19, 2014 at 15:25
  • @Boann Definitely no harm in asking. Drop a link here if you end up writing one!
    – dav_i
    Commented Aug 19, 2014 at 18:08
  • I really think you should just copy paste this to SO, answer it, accept your answer, mark as community wiki - then ask in here for a mod to flag it as protected (or flag it yourself for moderator from SO). You deserve the rep. Commented Aug 20, 2014 at 7:13
  • @BurhanKhalid If he deserves the reputation why are you suggesting to make it community wiki? AFAIK CW is for community effort posts. The OP doesn't seem to need the community to provide a great question & answer so I don't see why he should mark his Q&A as CW.
    – Bakuriu
    Commented Aug 20, 2014 at 11:16
  • You are right Bakuriu - but it should definitely be protected. Commented Aug 20, 2014 at 13:13

2 Answers 2

62

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...

You seem to be pretty knowledgeable on the subject. Why not write a Q&A yourself and Community Wiki it? You basically have the startings of both in the post you've just written here!

1
  • 20
    I wouldn't mark it community wiki for two reasons: 1. If you take the time to produce a great Q&A, you deserve the reputation you get from it. 2. It lowers the bar for edits, which isn't always ideal on a complex topic. Commented Aug 20, 2014 at 10:34
6

I've written a Q&A as suggested: Loop doesn't see changed value without a print statement

(I tried to mark the other questions as duplicates of it but the review queue doesn't work well so all the close votes just "expire".)

1
  • I've added a reference to this question from the java tag FAQs. Commented Mar 23, 2015 at 8:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .