-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
149 lines (117 loc) · 4 KB
/
Solution.java
File metadata and controls
149 lines (117 loc) · 4 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//I have therefore my boasting in Christ Jesus in things pertaining to God. (John 15:17)
package com.javarush.task.task17.task1722;
/*
Посчитаем
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
Counter counter1 = new Counter();
Counter counter2 = new Counter();
Counter counter3 = new Counter();
Counter counter4 = new Counter();
counter1.start();
counter1.join();
counter2.start();
counter2.join();
counter3.start();
counter3.join();
counter4.start();
counter4.join();
for (int i = 1; i <= 100; i++) {
if (values[i] != 1) {
System.out.println("Массив values содержит элементы неравные 1");
break;
}
}
}
public static Integer count = 0;
public static int[] values = new int[105];
static {
for (int i = 0; i < 105; i++) {
values[i] = 0;
}
}
public static void incrementCount() {
count++;
}
public static int getCount() {
return count;
}
public static class Counter extends Thread {
@Override
public void run() {
do {
synchronized (Solution.class) {
incrementCount();
values[getCount()]++;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
} while (getCount() < 100);
}
}
}
/*
Посчитаем
1. Сделай так, чтобы результат успел посчитаться для всех элементов массива values НЕ используя Thread.sleep в методе main(String[] args)
2. Исправь synchronized блок так, чтобы массив values заполнился значением 1
Требования:
1. Класс Solution должен содержать класс Counter.
2. Класс Counter должен быть нитью.
3. Метод run() класса Counter должен содержать synchronized блок.
4. Synchronized блок метода run() не должен блокировать мьютекс this.
5. Метод main(String[] args) класса Solution не должен использовать Thread.sleep().
6. Для каждой нити, в методе main(String[] args) класса Solution используй метод, который ожидает завершение нити.
package com.javarush.task.task17.task1722;
*
Посчитаем
*
public class Solution {
public static void main(String[] args) throws InterruptedException {
Counter counter1 = new Counter();
Counter counter2 = new Counter();
Counter counter3 = new Counter();
Counter counter4 = new Counter();
counter1.start();
counter2.start();
counter3.start();
counter4.start();
for (int i = 1; i <= 100; i++) {
if (values[i] != 1) {
System.out.println("Массив values содержит элементы неравные 1");
break;
}
}
}
public static Integer count = 0;
public static int[] values = new int[105];
static {
for (int i = 0; i < 105; i++) {
values[i] = 0;
}
}
public static void incrementCount() {
count++;
}
public static int getCount() {
return count;
}
public static class Counter extends Thread {
@Override
public void run() {
do {
synchronized (this) {
incrementCount();
values[getCount()]++;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
} while (getCount() < 100);
}
}
}
*/