NOTE
6.37 Deadlock
Example and status inspection.
This is a historical learning note and may contain outdated or incomplete understanding.
Example
public class DeadLock
{
private static final Object LOCK1 = new Object();
private static final Object LOCK2 = new Object();
public static void main(String[] args) throws InterruptedException
{
Thread thread1 = new Thread(()->{
synchronized (LOCK1)
{
try
{
TimeUnit.SECONDS.sleep(3);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
synchronized (LOCK2)
{
System.out.println(Thread.currentThread().getName() + ": acquired both locks");
}
}
});
Thread thread2 = new Thread(()->{
synchronized (LOCK2)
{
try
{
TimeUnit.SECONDS.sleep(3);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
synchronized (LOCK1)
{
System.out.println(Thread.currentThread().getName() + ": acquired both locks");
}
}
});
thread1.start();
thread2.start();
thread1.join();
thread2.join();
}
}
- View status


Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub