Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I assume 'no data race' between user thread and finalize thread in Java
    primarykey
    data
    text
    <p>Consider the following Java class:</p> <pre><code>class X { public void foo() { bar = 1; } protected void finalize() { if (bar == 1) baz(); } private int bar = 0; } </code></pre> <p>Under the assumption that <code>X.foo()</code> is never called from any <code>finalize()</code> method (directly or indirectly), can I be sure that the code above is free of data-races, that is, can I be sure that <code>X.finalize()</code> sees the value written by <code>X.foo()</code> in every case where <code>X.foo()</code> is actually called?</p> <p>The naive analysis would say that <code>X.finalize()</code> cannot run concurrently with <code>X.foo()</code> (due to the mentioned assumption), so no extra synchronization is necessary.</p> <p>I would guess that the code above is free of data races, but it troubles me that the language specification contains the following explicit statement in <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4.5" rel="nofollow">§17.4.5</a>, but says nothing about a happens-before relationship between finalize() and methods in general:</p> <blockquote> <p>There is a happens-before edge from the end of a constructor of an object to the start of a finalizer (§12.6) for that object.</p> </blockquote> <p><strong>EDIT:</strong> I see the need to make my question more precise, so here is an attempt at a precise reformulation of the question:</p> <p>Does Java guarantee a <em>happens-before</em> relation between a specific method <code>X.foo()</code> and <code>X.finalize()</code> if I guarantee that <code>X.foo()</code> is never called (directly or indirectly) from any <code>finalize()</code> method? Here, <em>happens-before</em> is to be interpreted exactly as defined in <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4.5" rel="nofollow">§17.4.5</a>.</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