Note that there are some explanatory texts on larger screens.

plurals
  1. POjvm reordering/visibility effect test
    primarykey
    data
    text
    <p>While writing some java article I'm trying to reproduce <a href="http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#reordering" rel="nofollow">re-ordering</a> in case of unsynchronized object costruction in multithreaded environment. The case when a heavy object is constructed w/o synchonization/volatiles/finals and other threads get access to it right after constructor call. Here is the code I try:</p> <pre><code>public class ReorderingTest { static SomeObject&lt;JPanel&gt;[] sharedArray = new SomeObject[100]; public static void main(String[] args) { for (int i = 0; i &lt; 100; i++) { String name = "watcher" + i; new Thread(new Watcher(name)).start(); System.out.printf("watcher %s started!%n", name); } } static class Watcher implements Runnable { private String name; Watcher(String name) { this.name = name; } public void run() { while (true) { int randomIndex = (int) (Math.random() * sharedArray.length); SomeObject&lt;JPanel&gt; item = sharedArray[randomIndex]; if (item == null) { //System.out.printf("sharedArray[%s]=null%n", randomIndex); double r = 1 + Math.random() * 1000; sharedArray[randomIndex] = new SomeObject&lt;JPanel&gt;( new JPanel(), UUID.randomUUID().toString(), r, (float)r * 33, (long)r); } else { //System.out.printf("sharedArray[%s]=&lt;obj&gt;!%n", randomIndex); if (item.value == null || (item.stringField == null) || (item.doubleField == 0) || (item.floatField == 0) || (item.longField == 0) ) { System.err.printf("watcher %s sees default values: %s!%n", name, item); } else { // fully initialized! run new construction process double r = 1 + Math.random() * 1000; sharedArray[randomIndex] = new SomeObject&lt;JPanel&gt;( new JPanel(), UUID.randomUUID().toString(), r, (float)r * 37, (long)r); } } /*try { TimeUnit.NANOSECONDS.sleep(randomIndex); } catch (InterruptedException e) { throw new RuntimeException(e); }*/ } } } static class SomeObject&lt;V&gt; { V value; String stringField; double doubleField; float floatField; long longField; SomeObject(V value, String stringField, double doubleField, float floatField, long longField) { this.value = value; this.stringField = stringField; this.doubleField = doubleField; this.floatField = floatField; this.longField = longField; } @Override public String toString() { return "SomeObject{" + "value=" + value == null ? "null" : "&lt;obj&gt;" + ", stringField='" + stringField + '\'' + ", doubleField=" + doubleField + ", floatField=" + floatField + ", longField=" + longField + '}'; } } } </code></pre> <p>-But no effect so far, I've tried on different 2,4 and 8 core Intel/AMD PCs with windows, ran test for a few hours - no reordering effect - System.err.printf("watcher %s sees ...") - is not called, static sharedArray[randomIndex] reference always contains fully constructed values.</p> <p>What's wrong? How to reproduce this? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    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