Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Static initializer deadlock proof of concept
package com.fsecure.pavlukhin.concurrentclinit;
abstract public class AbstractChildHolder {
public static class Child1 extends AbstractChildHolder {
static {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void foo() {
System.out.println(Thread.currentThread() + ": foo!");
}
}
public static class Child2 extends AbstractChildHolder {
static {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void foo() {
System.out.println(Thread.currentThread() + ": foo?");
}
}
public static final AbstractChildHolder C1 = new Child1();
public static final AbstractChildHolder C2 = new Child2();
abstract public void foo();
}
package com.fsecure.pavlukhin.concurrentclinit;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/*
* Deadlock while static initialization proof of concept
*/
public class AbstractChildHolderTest {
@Test(timeout = 5000L)
public void concurrentInitialization() throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<?> f1 = executor.submit(() -> new AbstractChildHolder.Child1().foo());
Future<?> f2 = executor.submit(() -> new AbstractChildHolder.Child2().foo());
f1.get();
f2.get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.