forked from JavaWebinar/basejava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainConcurrency.java
More file actions
136 lines (119 loc) · 4.3 KB
/
MainConcurrency.java
File metadata and controls
136 lines (119 loc) · 4.3 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
package ru.javawebinar.basejava;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* gkislin
* 29.08.2016
*/
public class MainConcurrency {
public static final int THREADS_NUMBER = 10000;
private int counter;
private final AtomicInteger atomicCounter = new AtomicInteger();
// private static final Object LOCK = new Object();
// private static final Lock lock = new ReentrantLock();
private static final ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
private static final Lock WRITE_LOCK = reentrantReadWriteLock.writeLock();
private static final Lock READ_LOCK = reentrantReadWriteLock.readLock();
private static final ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat();
}
};
public static void main(String[] args) throws InterruptedException {
System.out.println(Thread.currentThread().getName());
Thread thread0 = new Thread() {
@Override
public void run() {
System.out.println(getName() + ", " + getState());
// throw new IllegalStateException();
}
};
thread0.start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ", " + Thread.currentThread().getState());
}
private void inc() {
synchronized (this) {
// counter++;
}
}
}).start();
System.out.println(thread0.getState());
final MainConcurrency mainConcurrency = new MainConcurrency();
CountDownLatch latch = new CountDownLatch(THREADS_NUMBER);
ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
// CompletionService completionService = new ExecutorCompletionService(executorService);
//
// List<Thread> threads = new ArrayList<>(THREADS_NUMBER);
for (int i = 0; i < THREADS_NUMBER; i++) {
Future<Integer> future = executorService.submit(() ->
// Thread thread = new Thread(() ->
{
for (int j = 0; j < 100; j++) {
mainConcurrency.inc();
System.out.println(threadLocal.get().format(new Date()));
}
latch.countDown();
return 5;
});
// thread.start();
// threads.add(thread);
}
/*
threads.forEach(t -> {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
*/
latch.await(10, TimeUnit.SECONDS);
executorService.shutdown();
// System.out.println(mainConcurrency.counter);
System.out.println(mainConcurrency.atomicCounter.get());
// final String lock1 = "lock1";
// final String lock2 = "lock2";
// deadLock(lock1, lock2);
// deadLock(lock2, lock1);
}
private static void deadLock(Object lock1, Object lock2) {
new Thread(() -> {
System.out.println("Waiting " + lock1);
synchronized (lock1) {
System.out.println("Holding " + lock1);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Waiting " + lock2);
synchronized (lock2) {
System.out.println("Holding " + lock2);
}
}
}).start();
}
private void inc() {
// synchronized (this) {
// synchronized (MainConcurrency.class) {
// WRITE_LOCK.lock();
// try {
atomicCounter.incrementAndGet();
// counter++;
// } finally {
// WRITE_LOCK.unlock();
// }
// wait();
// readFile
// ...
// }
}
}