-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainConcurrency.java
More file actions
59 lines (52 loc) · 1.78 KB
/
MainConcurrency.java
File metadata and controls
59 lines (52 loc) · 1.78 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
package com.urise.webapp;
import java.util.ArrayList;
import java.util.List;
import static java.lang.Thread.currentThread;
public class MainConcurrency {
public static final int THREADS_NUMBER = 1_00;
private static int counter;
private static final Object LOCK1 = new Object();
private static final Object LOCK2 = new Object();
public static void main(String[] args) throws InterruptedException {
System.out.println(currentThread().getName());
Thread thread0 = new Thread() {
@Override
public void run() {
System.out.println(getName() + ", " + getState());
throw new IllegalStateException();
}
};
thread0.start();
new Thread(() -> System.out.println(currentThread().getName() + ", " + currentThread().getState())).start();
Thread.sleep(1000);
System.out.println(thread0.getState());
final MainConcurrency mainConcurrency = new MainConcurrency();
List<Thread> threads = new ArrayList<>(THREADS_NUMBER);
for (int i = 0; i < THREADS_NUMBER; i++) {
Thread thread = new Thread(() -> {
for (int j = 0; j < 100; j++) {
mainConcurrency.inc();
}
});
thread.start();
threads.add(thread);
}
threads.forEach(t -> {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println(counter);
}
private synchronized void inc() {
// synchronized (this) {
// synchronized (MainConcurrency.class) {
counter++;
// wait();
// readFile
// ...
// }
}
}