public class DeadLockExample {
FirstClass firstClass = new FirstClass();
SecondClass secondClass = new SecondClass();
public static void main(String args[]) {
DeadLockExample deadLockExample = new DeadLockExample();
ThreadOne t1 = deadLockExample.new ThreadOne();
Thread t2 = deadLockExample.new ThreadSecond();
t1.start();
t2.start();
}
class FirstClass {
}
class SecondClass {
}
class ThreadOne extends Thread {
public void run() {
synchronized (firstClass) {
try {
Thread.sleep(1000);
System.out.println(" Thread 1 holding lock 1");
} catch (InterruptedException e) {
System.out.println(" Thread 1 : waiting for lock 2");
e.printStackTrace();
}
// / System.out.println(" Thread accessing is "+
// Thread.currentThread().getName());
}
synchronized (secondClass) {
System.out.println(" Thread accessing is "
+ Thread.currentThread().getName());
}
}
}
class ThreadSecond extends Thread {
public void run() {
synchronized (secondClass) {
try {
Thread.sleep(1000);
System.out.println(" Thread 2 holding lock 1");
} catch (InterruptedException e) {
System.out.println(" Thread 1 : waiting for lock 2");
e.printStackTrace();
}
// System.out.println(" Thread accessing is "+
// Thread.currentThread().getName());
}
synchronized (firstClass) {
System.out.println(" Thread accessing is "
+ Thread.currentThread().getName());
}
}
}
}
No comments:
Post a Comment