Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Visibility</strong> problems can occur when a value your program has written to a variable is cached in the <strong>cpu cache</strong> and not written to RAM immediately. For this reason if thread A running on cpu A is writting a value without correct synchronization and thread B reads this value from cpu B he might see a stale value in RAM and not the most recent value (present only in the cpu cache of processor A).</p> <p>In the example given you are using none of the mechanisms Java provides to ensure safe publication. That is <strong>synchronization</strong>, <strong>volatile</strong> or <strong>final fields</strong> set in the constructor.</p> <p>Therefore one could imagine that in your example the reference to the create <code>clone</code> object becomes availabe but the values written to clones fields remains in the cpu cache. In this case other threads would not be able to read the up to date values.</p> <p>To give some reference. Look at this example</p> <pre> class FinalFieldExample { final int x; int y; static FinalFieldExample f; public FinalFieldExample() { x = 3; y = 4; } static void writer() { f = new FinalFieldExample(); } static void reader() { if (f != null) { int i = f.x; int j = f.y; } } } </pre> <blockquote> <p>The class above is an example of how final fields should be used. A thread executing reader is guaranteed to see the value 3 for f.x, because it is final. It is not guaranteed to see the value 4 for y, because it is not final.</p> </blockquote> <p>The argument you are making would hold for this example as well, wouldn't it? The instance is created, fields set in the constructor etc. However it is not thread-safe, since the value written to <code>y</code> needs not become visible to other threads. (The cited example is from <em>JSR 133 (Java Memory Model) FAQ</em>: <a href="http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#reordering" rel="nofollow noreferrer">http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#reordering</a>)</p> <p><strong>Update:</strong> You have asked for code that demonstrates the problem. I asked a similar (more open) question once: <a href="https://stackoverflow.com/q/2787094/136247">How to demonstrate java multithreading visibility problems?</a> The interesting thing with the code sample given is, that it will behave differently with <strong>different minor versions of Java, on different operating systems and wether using the client or server jvm</strong>. In that regard I found the sample quite interesting. What is important to note is, that it might well be impossible to actually create sample code that leads to a visibility problem with your code <strong>today</strong>. However next year cpu generation might implement a different caching policy and suddenly the problem appears. If you follow the guidelines of the Java Language Specification your save.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload