The thread that first refers to a class needs to initialize it. For instance, consider this interface:
interface Constants { int[] IARRAY = { 42 }; }
The thread that first refers to this interface will then initialize it;
among other things, it will need to create an array with one integer
element, assign the value 42 to this element, and store the reference in
the variable IARRAY
.
Now, suppose that the following class uses the previous interface:
class Foo implements Runnable { public static void main(String[] args) { Foo f = new Foo(); Thread t1 = new Thread(f), t2 = new Thread(f); t1.start(); t2.start(); } public void run() { System.out.println(Constants.IARRAY[0]); } }
Suppose that t1
is the first thread that reaches the line
where the value of IARRAY[0]
is printed. Then,
t1
needs to initialize the interface
Constants
and, according to the rules of Java,
if t2
also reaches this line, it must wait until the
initialization is finished (that is, t2
cannot initialize the
interface Constants
if another thread is already doing that,
nor is t2
allowed to see the value of IARRAY[0]
while the initialization is not over).
A problem with the algorithm Dinning-Schonberg is that it
is not aware of this Java rule, and for a code like the one above it
would issue a alarm about a race condition that, in fact, does not exist.
You can check the option Ignore class initialization to prevent
false alarms about race conditions in cases like the one described above,
but then you may miss a real race condition, like in the following example
(there is a race condition involving the variable Main.pi
):
interface Y { float PI = Main.pi; } class Main { static float pi = 2.14; public static void main(String[] args) { new Thread() { public void run() { System.out.println(Y.PI); // may print 2.14 or 3.14 } } Main.pi++; } }The option for ignoring class initialization does not make sense for Eraser (which automatically ignores variable initialization, in particular if they occur while a class is initialized), but note that Eraser can potentially miss a race condition like the one shown in the previous example.
Back.