-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
102 lines (81 loc) · 2.83 KB
/
Copy pathSolution.java
File metadata and controls
102 lines (81 loc) · 2.83 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
//His disciples remembered that it was written, "Zeal for your house will eat me up." (John 2:17)
package com.javarush.task.task27.task2704;
/*
Модификаторы и deadlock
*/
public class Solution {
private final String field;
public Solution(String field) {
this.field = field;
}
public synchronized String getField() {
return field;
}
public synchronized void sout(Solution solution) {
System.out.format("111: %s: %s %n", this.field, solution.getField());
solution.sout2(this);
}
public synchronized void sout2(Solution solution) {
System.out.format("222: %s: %s %n", this.field, solution.getField());
solution.sout(this);
}
public static void main(String[] args) {
final Solution solution = new Solution("first");
final Solution solution2 = new Solution("second");
new Thread(new Runnable() {
public void run() {
solution.sout(solution2);
}
}).start();
new Thread(new Runnable() {
public void run() {
solution2.sout(solution);
}
}).start();
}
}
/*
Модификаторы и deadlock
Расставь модификаторы так, чтобы при работе с этим кодом появился deadlock.
Метод main порождает deadlock, поэтому не участвует в тестировании.
Требования:
1. Поле field должно быть приватным.
2. Метод getField НЕ должен быть приватным.
3. Метод sout должен быть объявлен с модификатором synchronized.
4. Метод sout2 должен быть объявлен с модификатором synchronized.
package com.javarush.task.task27.task2704;
*
Модификаторы и deadlock
*
public class Solution {
private final String field;
public Solution(String field) {
this.field = field;
}
public String getField() {
return field;
}
public void sout(Solution solution) {
System.out.format("111: %s: %s %n", this.field, solution.getField());
solution.sout2(this);
}
public void sout2(Solution solution) {
System.out.format("222: %s: %s %n", this.field, solution.getField());
solution.sout(this);
}
public static void main(String[] args) {
final Solution solution = new Solution("first");
final Solution solution2 = new Solution("second");
new Thread(new Runnable() {
public void run() {
solution.sout(solution2);
}
}).start();
new Thread(new Runnable() {
public void run() {
solution2.sout(solution);
}
}).start();
}
}
*/