Created
October 23, 2014 12:48
-
-
Save yurylyt/7862e7d42b2c39116f7f to your computer and use it in GitHub Desktop.
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
public class Deadlock { | |
public static void main(String[] args) { | |
final Integer mutex1 = 1; | |
final Integer mutex2 = 2; | |
new Thread(new DeadlockRunnable(mutex1, mutex2)).start(); | |
new Thread(new DeadlockRunnable(mutex2, mutex1)).start(); | |
} | |
static class DeadlockRunnable implements Runnable { | |
private final Integer first; | |
private final Integer second; | |
DeadlockRunnable(Integer first, Integer second) { | |
this.first = first; | |
this.second = second; | |
} | |
@Override | |
public void run() { | |
synchronized (first) { | |
try { | |
Thread.sleep(100); // wait a bit to allow other thread catch on a mutex | |
} catch (InterruptedException e) { | |
// ignore | |
} | |
synchronized (second) { | |
System.out.println("I'm not locked!"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment