Skip to content

Instantly share code, notes, and snippets.

@asela38
Last active May 15, 2018 05:28
Show Gist options
  • Save asela38/5b5b3e75c1e0a09aff31c5c6ef7e18ad to your computer and use it in GitHub Desktop.
Save asela38/5b5b3e75c1e0a09aff31c5c6ef7e18ad to your computer and use it in GitHub Desktop.
Java Code To Create Dead Lock
public class DeadLockTest {
private static Object o1 = "Lock 1" , o2 = "Lock 2";
public static void main(String[] args) {
new Thread() {
public void run(){
synchronized (o2) {
print();
synchronized (o1) {
print();
}
}
}
}.start();
synchronized (o1) {
print();
synchronized (o2) {
print();
}
}
}
public static void print() {
System.out.printf("%s %n has %s : %s %n has %s : %s %n", Thread.currentThread().getName(),
o1, Thread.holdsLock(o1),
o2, Thread.holdsLock(o2));
}
}
@asela38
Copy link
Author

asela38 commented May 15, 2018

Simple code to create make a deadlock in an old-fashioned way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment