Skip to content

Instantly share code, notes, and snippets.

@yurylyt
Created October 23, 2014 12:48
Show Gist options
  • Save yurylyt/7862e7d42b2c39116f7f to your computer and use it in GitHub Desktop.
Save yurylyt/7862e7d42b2c39116f7f to your computer and use it in GitHub Desktop.
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