forked from JavaOPs/basejava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainConcurrancy.java
More file actions
157 lines (136 loc) · 5.19 KB
/
MainConcurrancy.java
File metadata and controls
157 lines (136 loc) · 5.19 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
150
151
152
153
154
155
156
157
package ru.javaops.webapp;
import ru.javaops.webapp.util.LazySingleton;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class MainConcurrancy {
private static int counter;
//private static final Object LOCK = new Object();
private static final Lock lock = new ReentrantLock();
private final static AtomicInteger atomicCounter = new AtomicInteger();
private static final int THREADS_COUNT = 10000;
private static final ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>(){
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat();
}
};
public static void main(String[] args) throws InterruptedException {
Thread thread0 = new Thread() {
public void run() {
System.out.println(getName() + ", " + getState());
}
};
thread0.start();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
Thread currentThred = Thread.currentThread();
System.out.println(currentThred.getName() + ", " + currentThred.getState());
// throw new IllegalStateException();
}
});
thread1.start();
new Thread(() -> {
Thread currentThread = Thread.currentThread();
System.out.println(currentThread.getName() + ", " + currentThread.getState());
}).start();
System.out.println(thread0.getName() + ", " + thread0.getState());
System.out.println(thread1.getName() + ", " + thread1.getState());
//Thread.currentThread().sleep(500);
//Thread.currentThread().join();
//System.out.println(counter);
//final Object lock2 = new Object();
final MainConcurrancy mainConcurancy = new MainConcurrancy();
//CountDownLatch latch = new CountDownLatch(THREADS_COUNT);
//Создать кол-во потоков равное кол-ву ядер на ПК/Сервере
// ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
ExecutorService executorService = Executors.newCachedThreadPool();
// CompletionService completionService = new ExecutorCompletionService(executorService);
// List<Thread> threads = new ArrayList<>(THREADS_COUNT);
for (int i = 0; i < THREADS_COUNT; i++) {
Future<Integer> future = executorService.submit(() -> {
{
for (int j = 0; j < 100; j++) {
mainConcurancy.inc();
//System.out.println(threadLocal.get().format(new Date()));
}
//latch.countDown();
return 5;
}
});
// completionService.poll();
/*System.out.println(future.isDone());
try {
System.out.println(future.get());
} catch (ExecutionException e) {
e.printStackTrace();
}*/
/*Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 100; j++) {
mainConcurancy.inc();
}
latch.countDown();
}
});
thread.start();*/
//thread.join();
// threads.add(thread);
}
//System.out.println(counter);
System.out.println(atomicCounter.get());
// threads.forEach(thread -> {
// try {
// thread.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// });
//latch.await(10, TimeUnit.SECONDS);
executorService.shutdown();
//Thread.sleep(500);
LazySingleton.getInstance();
//System.out.println(counter);
System.out.println(atomicCounter.get());
String lock1 = "lock1";
String lock2 = "lock2";
// doDeadLock(lock1, lock2);
// doDeadLock(lock2, lock1);
}
private static void doDeadLock(String lock1, String lock2) {
new Thread(() -> {
synchronized (lock1){
System.out.println("Holding lock1");
System.out.println("Waiting lock2");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock2){
System.out.println("Holding lock2");
}
}
}).start();
}
private void inc() {
double sin = Math.sin(123);
atomicCounter.incrementAndGet();
// lock.lock();
// try {
// counter++;
// } finally {
// lock.unlock();
// }
/*synchronized (this) {
counter++;
}*/
}
}