11package ru .javawebinar .basejava ;
22
3- import java .util .ArrayList ;
4- import java .util .List ;
5-
3+ import java .text .SimpleDateFormat ;
4+ import java .util .Date ;
5+ import java .util .concurrent .*;
6+ import java .util .concurrent .atomic .AtomicInteger ;
7+ import java .util .concurrent .locks .Lock ;
8+ import java .util .concurrent .locks .ReentrantReadWriteLock ;
9+
10+ /**
11+ * gkislin
12+ * 29.08.2016
13+ */
614public class MainConcurrency {
715 public static final int THREADS_NUMBER = 10000 ;
816 private int counter ;
9- private static final Object LOCK = new Object ();
17+ private final AtomicInteger atomicCounter = new AtomicInteger ();
18+
19+ // private static final Object LOCK = new Object();
20+ // private static final Lock lock = new ReentrantLock();
21+ private static final ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock ();
22+ private static final Lock WRITE_LOCK = reentrantReadWriteLock .writeLock ();
23+ private static final Lock READ_LOCK = reentrantReadWriteLock .readLock ();
24+ private static final ThreadLocal <SimpleDateFormat > threadLocal = new ThreadLocal <SimpleDateFormat >() {
25+ @ Override
26+ protected SimpleDateFormat initialValue () {
27+ return new SimpleDateFormat ();
28+ }
29+ };
1030
1131 public static void main (String [] args ) throws InterruptedException {
1232 System .out .println (Thread .currentThread ().getName ());
@@ -15,7 +35,7 @@ public static void main(String[] args) throws InterruptedException {
1535 @ Override
1636 public void run () {
1737 System .out .println (getName () + ", " + getState ());
18- throw new IllegalStateException ();
38+ // throw new IllegalStateException();
1939 }
2040 };
2141 thread0 .start ();
@@ -38,32 +58,46 @@ private void inc() {
3858 System .out .println (thread0 .getState ());
3959
4060 final MainConcurrency mainConcurrency = new MainConcurrency ();
41- List <Thread > threads = new ArrayList <>(THREADS_NUMBER );
61+ CountDownLatch latch = new CountDownLatch (THREADS_NUMBER );
62+ ExecutorService executorService = Executors .newFixedThreadPool (Runtime .getRuntime ().availableProcessors ());
63+ // CompletionService completionService = new ExecutorCompletionService(executorService);
64+ //
65+ // List<Thread> threads = new ArrayList<>(THREADS_NUMBER);
4266
4367 for (int i = 0 ; i < THREADS_NUMBER ; i ++) {
44- Thread thread = new Thread (() -> {
68+
69+ Future <Integer > future = executorService .submit (() ->
70+ // Thread thread = new Thread(() ->
71+ {
4572 for (int j = 0 ; j < 100 ; j ++) {
4673 mainConcurrency .inc ();
74+ System .out .println (threadLocal .get ().format (new Date ()));
4775 }
76+ latch .countDown ();
77+ return 5 ;
4878 });
49- thread .start ();
50- threads .add (thread );
79+ // thread.start();
80+ // threads.add(thread);
5181 }
5282
83+ /*
5384 threads.forEach(t -> {
5485 try {
5586 t.join();
5687 } catch (InterruptedException e) {
5788 e.printStackTrace();
5889 }
5990 });
60- System .out .println (mainConcurrency .counter );
61-
62- final String lock1 = "lock1" ;
63- final String lock2 = "lock2" ;
64- deadLock (lock1 , lock2 );
65- deadLock (lock2 , lock1 );
66-
91+ */
92+ latch .await (10 , TimeUnit .SECONDS );
93+ executorService .shutdown ();
94+ // System.out.println(mainConcurrency.counter);
95+ System .out .println (mainConcurrency .atomicCounter .get ());
96+
97+ // final String lock1 = "lock1";
98+ // final String lock2 = "lock2";
99+ // deadLock(lock1, lock2);
100+ // deadLock(lock2, lock1);
67101 }
68102
69103 private static void deadLock (Object lock1 , Object lock2 ) {
@@ -84,10 +118,16 @@ private static void deadLock(Object lock1, Object lock2) {
84118 }).start ();
85119 }
86120
87- private synchronized void inc () {
121+ private void inc () {
88122// synchronized (this) {
89123// synchronized (MainConcurrency.class) {
90- counter ++;
124+ // WRITE_LOCK.lock();
125+ // try {
126+ atomicCounter .incrementAndGet ();
127+ // counter++;
128+ // } finally {
129+ // WRITE_LOCK.unlock();
130+ // }
91131// wait();
92132// readFile
93133// ...
0 commit comments