Last active
May 15, 2018 05:28
-
-
Save asela38/5b5b3e75c1e0a09aff31c5c6ef7e18ad to your computer and use it in GitHub Desktop.
Java Code To Create Dead Lock
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 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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple code to create make a deadlock in an old-fashioned way.