Created
April 9, 2015 15:33
-
-
Save TanyaGaleyev/36b68b0d7150972dab43 to your computer and use it in GitHub Desktop.
Static initializer deadlock proof of concept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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