Created
March 29, 2016 21:41
-
-
Save eribeiro/bd2aca6fbae0255b64ba0b4f896e6a8d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
{ | |
public static void main(String[] args) | |
{ | |
final Foo a = new Foo(); | |
final Foo b = new Foo(); | |
new Thread(new Runnable() | |
{ | |
@Override | |
public void run() | |
{ | |
sleepALitte(); | |
a.equals(b); | |
} | |
}).start(); | |
new Thread(new Runnable() | |
{ | |
@Override | |
public void run() | |
{ | |
// sleep some time before firing threads | |
// as to sync | |
sleepALitte(); | |
b.equals(a); | |
} | |
}).start(); | |
} | |
private static void sleepALitte() | |
{ | |
try | |
{ | |
Thread.sleep(3000); | |
} | |
catch (InterruptedException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
static class Foo { | |
public synchronized boolean equals(Object o) | |
{ | |
System.out.println("trying to get lock"); | |
synchronized (o) { | |
/* IF you are not getting the deadlock then | |
try to uncomment the lines below */ | |
// System.out.println("do stuff"); | |
// sleepOneSecond(); | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment