Skip to content

Instantly share code, notes, and snippets.

@eribeiro
Created March 29, 2016 21:41
Show Gist options
  • Save eribeiro/bd2aca6fbae0255b64ba0b4f896e6a8d to your computer and use it in GitHub Desktop.
Save eribeiro/bd2aca6fbae0255b64ba0b4f896e6a8d to your computer and use it in GitHub Desktop.
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