I am facing a weird problem in one of our projects. We use JUnit to run our unit tests, and some time ago, we started to run pur tests in parallel to speed up execution. Most of the time, all is fine, but from time to time, nearly all of our tests fail. On the next run, they all pass again without any code being changed.
The errors seem to indicate that some static instances are not initialised correctly or used before initialisation is completed in the multithreaded case. (I cannot debug this, since the problem never once showed up when debugging -> Heisenbug.)
Sorry I cannot provide a minimal working example showing the bug because it disappears when trying to reproduce.
The concrete question is: when declaring a variable like below, is there any chance initialisation of a or b hasn't completed when foo() or bar() are called by another thread? I thought the static block would be guaranteed to execute before any of the methods can be called. Or can there be class loader issues? Or known errors in the JRE (we are currently stuck at 1.6.0_21, newer versions are not yet provided by our IT department)?
class C {
private static final A a;
private static final B b;
static {
a = new A(...);
b = new B(...);
}
public static void foo() {
useA();
}
public static void bar() {
useB();
}
}
I am sure it's not hardware related since it shows up on different machines from different manufacturers. Tests are using the server vm.
Thank you,
Axel