Skip to content

Instantly share code, notes, and snippets.

@TanyaGaleyev
Created April 9, 2015 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TanyaGaleyev/36b68b0d7150972dab43 to your computer and use it in GitHub Desktop.
Save TanyaGaleyev/36b68b0d7150972dab43 to your computer and use it in GitHub Desktop.
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