Created
March 14, 2016 08:58
-
-
Save nkcoder/0be887e3ee453b1bcc44 to your computer and use it in GitHub Desktop.
java dead lock demo
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 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