-
Notifications
You must be signed in to change notification settings - Fork 1
/
Thread2.java
47 lines (43 loc) · 1.29 KB
/
Thread2.java
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
/**
* Второй поток
*/
class Thread2 implements Runnable {
@Override
public void run() {
int stringCount = 0;
for (int i = 0; i < 50000; ++i) {
Main.lock.increment();
Main.atomicInteger.incrementAndGet();
if (!Main.var2)
System.out.println("FALSE!");
/* synchronized (Main.strings) {
while (!Main.strings.isEmpty()) {
assert Main.strings.size() >= 1;
// String s = Main.strings.remove();
stringCount++;
}
} */
}
while (!Thread1.ready) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (!Main.strings.isEmpty()) {
String s = Main.strings.remove();
stringCount++;
}
System.out.println("stringCount = " + stringCount);
for (int i = 0; i < 12; ++i) {
System.out.println("Thread2: i = " + i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Поток 2 завершён!");
}
}