-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
109 lines (85 loc) · 2.96 KB
/
Solution.java
File metadata and controls
109 lines (85 loc) · 2.96 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//Now I say that Christ has been made a servant of the circumcision for the truth of God, that he might confirm the promises given to the fathers (Romans 15:8)
package com.javarush.task.task16.task1625;
/*
Взаимная блокировка
*/
public class Solution {
static Thread t1 = new T1();
static Thread t2 = new T2();
public static void main(String[] args) throws InterruptedException {
t1.start();
t1.interrupt();
t2.start();
t2.interrupt();
}
public static class T1 extends Thread {
@Override
public void run() {
try {
t2.join();
System.out.println("T1 finished");
} catch (InterruptedException e) {
System.out.println("T1 was interrupted");
}
}
}
public static class T2 extends Thread {
@Override
public void run() {
try {
t1.join();
System.out.println("T2 finished");
} catch (InterruptedException e) {
System.out.println("T2 was interrupted");
}
}
}
}
/*
Взаимная блокировка
1. Разберись, как работает программа.
2. Не меняя классы T1 и T2 сделай так, чтобы их нити завершились, не обязательно успешно.
3. Метод sleep не использовать.
Требования:
1. Метод main должен запускать нить t1.
2. Метод main должен запускать нить t2.
3. Класс T1 не изменять.
4. Класс T2 не изменять.
5. Метод sleep не использовать.
6. Вывод программы должен состоять из 2х строк, информирующих о завершении нитей. Например: "T1 was interrupted" и "T2 finished".
7. Нити t1 и t2 должны завершаться (не обязательно успешно).
package com.javarush.task.task16.task1625;
*
Взаимная блокировка
*
public class Solution {
static Thread t1 = new T1();
static Thread t2 = new T2();
public static void main(String[] args) throws InterruptedException {
t1.start();
t2.start();
}
public static class T1 extends Thread {
@Override
public void run() {
try {
t2.join();
System.out.println("T1 finished");
} catch (InterruptedException e) {
System.out.println("T1 was interrupted");
}
}
}
public static class T2 extends Thread {
@Override
public void run() {
try {
t1.join();
System.out.println("T2 finished");
} catch (InterruptedException e) {
System.out.println("T2 was interrupted");
}
}
}
}
*/