-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdead_lock
More file actions
53 lines (44 loc) · 1.52 KB
/
dead_lock
File metadata and controls
53 lines (44 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
````
package thread;
class FirstResource implements Runnable {
@Override
public void run() {
synchronized (String.class){
System.out.println(Thread.currentThread().getName() + " Thread entered in first resource");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " is waiting for object class ");
synchronized (Object.class){
}
System.out.println("Thread executed successfully "+ Thread.currentThread().getName());
}
}
}
class SecondResource implements Runnable{
public void run(){
synchronized (Object.class){
System.out.println(Thread.currentThread().getName() + " Thread entered in second resource");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+ " is waiting for String class ");
synchronized (String.class){
System.out.println("Thread executed sucessfully"+ Thread.currentThread().getName());
}
}
}
}
public class DeadLock {
public static void main(String[] args){
Thread t1 = new Thread(new FirstResource(), "Thread 1");
Thread t2 = new Thread(new SecondResource(), "Thread 2");
t1.start();
t2.start();
}
}
````