-
Notifications
You must be signed in to change notification settings - Fork 695
Open
Description
public class DeadlockExample {
public static Object Lock1 = new Object();
public static Object Lock2 = new Object();
public static void main(String[] args) {
ThreadDemo1 T1 = new ThreadDemo1();
ThreadDemo2 T2 = new ThreadDemo2();
T1.start();
T2.start();
}
private static class ThreadDemo1 extends Thread {
public void run() {
synchronized (Lock1) {
System.out.println("Thread 1: Has Lock1");
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
System.out.println("Thread 1: Waiting for Lock2");
synchronized (Lock2) {
System.out.println("Thread 1: Has Lock1 and Lock2");
}
}
}
}
private static class ThreadDemo2 extends Thread {
public void run() {
synchronized (Lock2) {
System.out.println("Thread 2: Has Lock2");
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
System.out.println("Thread 2: Waiting for Lock1");
synchronized (Lock1) {
System.out.println("Thread 2: Has Lock2 and Lock1");
}
}
}
}
}
Metadata
Metadata
Assignees
Labels
No labels