Note that there are some explanatory texts on larger screens.

plurals
  1. POin what architectures/OS other thread can see default nonfinal field values after constructor call?
    primarykey
    data
    text
    <p>I'm trying to reproduce a memory visibility issue in case of insufficient object initialization for non-final fields (JLS <a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/memory.html#17.5" rel="nofollow noreferrer">17.5 Final Field Semantics</a>, <a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/memory.html#65124" rel="nofollow noreferrer">FinalFieldExample</a> class example). Where it stated "However, f.y is not final; the reader() method is therefore not guaranteed to see the value 4 for it"</p> <p>I've tried this code:</p> <pre><code>public class ReorderingTest2 { public static void main(String[] args) { for (int i = 0; i &lt; 2500; i++) { new Thread(new Reader(i)).start(); new Thread(new Writer(i)).start(); } } static class Reader implements Runnable { private String name; Reader(int i) { this.name = "reader" + i; } @Override public void run() { //System.out.println(name + " started"); while (true) { FinalFieldExample.reader(name); } } } static class Writer implements Runnable { private String name; Writer(int i) { this.name = "writer" + i; } @Override public void run() { //System.out.println(name + " started"); while (true) { FinalFieldExample.writer(); } } } static class FinalFieldExample { int x; int y; static FinalFieldExample f; public FinalFieldExample() { x = 3; y = 4; } static void writer() { f = new FinalFieldExample(); } static void reader(String name) { if (f != null) { int i = f.x; int j = f.y; if (i != 3 || j != 4) { System.out.printf("reader %s sees it!%n", name); } } } } } </code></pre> <p>As in <a href="https://stackoverflow.com/questions/6264466/jvm-reordering-visibility-effect-test">previous my similar topic</a> - I've tried on different PCs (from 2 to 8 cores) with Windows and even on our server-side Solaris 32 core box - I couldn't reproduce it: f.x and f.y - are always already proper-initialized.</p> <p>For Intel/x86/x64 architecture as <a href="https://stackoverflow.com/questions/6264466/jvm-reordering-visibility-effect-test/6265793#6265793">I got the answer</a> - they have pretty much <a href="http://bartoszmilewski.wordpress.com/2008/11/05/who-ordered-memory-fences-on-an-x86/" rel="nofollow noreferrer">default memery guarantees</a> which prevent such constructor logic reordering. Seems the same is true for Solaris/sparc too?</p> <p>So in what architecture/OSes this reordering can be reproduced? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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