Skip to content

Instantly share code, notes, and snippets.

@nkcoder
Created March 14, 2016 08:58
Show Gist options
  • Save nkcoder/0be887e3ee453b1bcc44 to your computer and use it in GitHub Desktop.
Save nkcoder/0be887e3ee453b1bcc44 to your computer and use it in GitHub Desktop.
java dead lock demo
public class DeadLockDemo {
public static void main(String[] args) {
Object o1 = new Object();
Object o2 = new Object();
Runnable r1 = () -> {
synchronized(o1) {
try {
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
synchronized(o2) {
System.out.println("in r1...");
}
}
};
Runnable r2 = () -> {
synchronized(o2) {
try {
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
synchronized(o1) {
System.out.println("in r2...");
}
}
};
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
System.out.println("in main, ending...");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment