File tree Expand file tree Collapse file tree 13 files changed +221
-130
lines changed
src/main/java/java0/conc0303/homework Expand file tree Collapse file tree 13 files changed +221
-130
lines changed Original file line number Diff line number Diff line change 1919 </plugin >
2020 </plugins >
2121 </build >
22+ <dependencies >
23+ <dependency >
24+ <groupId >junit</groupId >
25+ <artifactId >junit</artifactId >
26+ <version >4.11</version >
27+ <scope >compile</scope >
28+ </dependency >
29+ </dependencies >
2230
2331
2432</project >
Original file line number Diff line number Diff line change 11package java0 .conc0303 .homework ;
22
3- import java .util .concurrent .Callable ;
4- import java .util .concurrent .ExecutorService ;
5- import java .util .concurrent .Executors ;
6- import java .util .concurrent .Future ;
7-
83/**
9- * 应用Future机制
4+ * 应用 volatile 机制
105 */
11- public class AsyncResultImpl1 implements AsyncResult {
6+ public class AsyncResultImpl1 implements AsyncResult {
7+ private volatile int result = -1 ;
8+ private volatile boolean hadComputed = false ;
129 @ Override
13- public int getResult () throws Exception {
14- ExecutorService executorService = Executors .newFixedThreadPool (1 );
15- Future <Integer > future = executorService .submit (new Callable <Integer >() {
10+ public int getResult () throws Exception {
11+ new Thread (new Runnable () {
1612 @ Override
17- public Integer call () {
18- return sum ();
13+ public void run () {
14+ result = sum ();
15+ hadComputed = true ;
1916 }
20- });
21- // 注意关闭线程池,否则main线程不会结束,会一直等待,直到线程池被关闭。
22- executorService .shutdown ();
23- return future .get ();
17+ }).start ();
18+
19+ while (!hadComputed ){
20+ Thread .yield ();// 防止一直占用线程
21+ }
22+ return result ;
2423 }
2524}
Original file line number Diff line number Diff line change 11package java0 .conc0303 .homework ;
22
3- import java .util .concurrent .locks . Condition ;
4- import java .util .concurrent .locks . ReentrantLock ;
3+ import java .util .concurrent .ArrayBlockingQueue ;
4+ import java .util .concurrent .BlockingQueue ;
55
6+ /**
7+ * 应用阻塞队列
8+ */
69public class AsyncResultImpl10 implements AsyncResult {
7- private volatile int result = -1 ;
8-
10+ private BlockingQueue <Integer > blockingQueue = new ArrayBlockingQueue <>(1 );
911 @ Override
1012 public int getResult () throws Exception {
11- final ReentrantLock lock = new ReentrantLock (false );
12- Condition hadCompute = lock .newCondition ();
1313 new Thread (new Runnable () {
1414 @ Override
1515 public void run () {
16- lock .lock ();
17- try {
18- result = sum ();
19- hadCompute .signalAll ();
20- }finally {
21- lock .unlock ();
22- }
16+ blockingQueue .offer (sum ());
2317 }
2418 }).start ();
2519
26- lock .lock ();
27- try {
28- hadCompute .await ();
29- return result ;
30- }finally {
31- lock .unlock ();
32- }
20+ return blockingQueue .take ();
3321 }
3422}
Original file line number Diff line number Diff line change 11package java0 .conc0303 .homework ;
22
3- import java .util .concurrent .CountDownLatch ;
3+ import java .util .concurrent .atomic .AtomicInteger ;
4+
5+ /**
6+ * 应用CAS机制
7+ */
8+ public class AsyncResultImpl2 implements AsyncResult {
49
5- public class AsyncResultImpl2 implements AsyncResult {
610 private volatile int result = -1 ;
11+
12+ private AtomicInteger tag = new AtomicInteger (1 );
13+
714 @ Override
815 public int getResult () throws Exception {
9- CountDownLatch countDownLatch = new CountDownLatch (1 );
1016 new Thread (new Runnable () {
1117 @ Override
1218 public void run () {
1319 result = sum ();
14- countDownLatch . countDown ();
20+ tag . getAndDecrement ();
1521 }
1622 }).start ();
17- countDownLatch .await ();
23+
24+ while (tag .get () > 0 ){
25+ Thread .yield ();
26+ }
1827 return result ;
1928 }
2029}
Original file line number Diff line number Diff line change 11package java0 .conc0303 .homework ;
22
33/**
4- * 应用传统的synchronized锁通知机制
4+ * 应用join 机制
55 */
66public class AsyncResultImpl3 implements AsyncResult {
77 private volatile int result = -1 ;
8- private final Object getResultLock = new Object ();
9-
108 @ Override
119 public int getResult () throws Exception {
12- new Thread (new Runnable () {
10+ Thread t1 = new Thread (new Runnable () {
1311 @ Override
1412 public void run () {
15- synchronized (getResultLock ) {
16- result = sum ();
17- getResultLock .notifyAll ();
18- }
13+ result = sum ();
1914 }
20- }).start ();
15+ });
16+ t1 .start ();
17+ t1 .join ();
2118
22- synchronized (getResultLock ) {
23- getResultLock .wait ();
24- return result ;
25- }
19+ return result ;
2620 }
2721}
Original file line number Diff line number Diff line change 11package java0 .conc0303 .homework ;
22
3- import java .util .concurrent .ArrayBlockingQueue ;
4- import java .util .concurrent .BlockingQueue ;
5-
63/**
7- * 应用阻塞队列
4+ * 应用传统的synchronized锁通知机制
85 */
96public class AsyncResultImpl4 implements AsyncResult {
10- private BlockingQueue <Integer > blockingQueue = new ArrayBlockingQueue <>(1 );
7+ private volatile int result = -1 ;
8+ private final Object getResultLock = new Object ();
9+
1110 @ Override
1211 public int getResult () throws Exception {
1312 new Thread (new Runnable () {
1413 @ Override
1514 public void run () {
16- blockingQueue .offer (sum ());
15+ synchronized (getResultLock ) {
16+ result = sum ();
17+ getResultLock .notifyAll ();
18+ }
1719 }
1820 }).start ();
1921
20- return blockingQueue .take ();
22+ synchronized (getResultLock ) {
23+ getResultLock .wait ();
24+ return result ;
25+ }
2126 }
2227}
Original file line number Diff line number Diff line change 11package java0 .conc0303 .homework ;
22
3- import java .util .concurrent .Semaphore ;
3+ import java .util .concurrent .locks .Condition ;
4+ import java .util .concurrent .locks .ReentrantLock ;
45
5- /**
6- * 应用Semaphore机制
7- */
86public class AsyncResultImpl5 implements AsyncResult {
97 private volatile int result = -1 ;
108
119 @ Override
1210 public int getResult () throws Exception {
13- final Semaphore semaphore = new Semaphore ( 2 );
14- semaphore . acquire ( 1 );
11+ final ReentrantLock lock = new ReentrantLock ( false );
12+ Condition hadCompute = lock . newCondition ( );
1513 new Thread (new Runnable () {
1614 @ Override
1715 public void run () {
16+ lock .lock ();
1817 try {
1918 result = sum ();
19+ hadCompute .signalAll ();
2020 }finally {
21- semaphore . release ( 1 );
21+ lock . unlock ( );
2222 }
2323 }
2424 }).start ();
2525
26- semaphore .acquire (2 );
27- return result ;
26+ lock .lock ();
27+ try {
28+ hadCompute .await ();
29+ return result ;
30+ }finally {
31+ lock .unlock ();
32+ }
2833 }
2934}
Original file line number Diff line number Diff line change 11package java0 .conc0303 .homework ;
22
3- import java .util .concurrent .CyclicBarrier ;
3+ import java .util .concurrent .CountDownLatch ;
44
55/**
6- * 应用 CyclicBarrier 机制
6+ * 应用CountDownLatch机制
77 */
8- public class AsyncResultImpl6 implements AsyncResult {
8+ public class AsyncResultImpl6 implements AsyncResult {
99 private volatile int result = -1 ;
1010 @ Override
1111 public int getResult () throws Exception {
12- final CyclicBarrier cyclicBarrier = new CyclicBarrier ( 2 );
12+ CountDownLatch countDownLatch = new CountDownLatch ( 1 );
1313 new Thread (new Runnable () {
1414 @ Override
1515 public void run () {
16- try {
17- result = sum ();
18- cyclicBarrier .await ();
19- } catch (Exception e ) {
20- e .printStackTrace ();
21- }
16+ result = sum ();
17+ countDownLatch .countDown ();
2218 }
2319 }).start ();
24- cyclicBarrier .await ();
20+ countDownLatch .await ();
2521 return result ;
2622 }
2723}
Original file line number Diff line number Diff line change 11package java0 .conc0303 .homework ;
22
3- import java .util .concurrent .atomic . AtomicInteger ;
3+ import java .util .concurrent .CyclicBarrier ;
44
55/**
6- * 应用CAS机制
6+ * 应用 CyclicBarrier 机制
77 */
88public class AsyncResultImpl7 implements AsyncResult {
9-
109 private volatile int result = -1 ;
11-
12- private AtomicInteger tag = new AtomicInteger (1 );
13-
1410 @ Override
1511 public int getResult () throws Exception {
12+ final CyclicBarrier cyclicBarrier = new CyclicBarrier (2 );
1613 new Thread (new Runnable () {
1714 @ Override
1815 public void run () {
19- result = sum ();
20- tag .getAndDecrement ();
16+ try {
17+ result = sum ();
18+ cyclicBarrier .await ();
19+ } catch (Exception e ) {
20+ e .printStackTrace ();
21+ }
2122 }
2223 }).start ();
23-
24- while (tag .get () > 0 ){
25- Thread .yield ();
26- }
24+ cyclicBarrier .await ();
2725 return result ;
2826 }
2927}
Original file line number Diff line number Diff line change 11package java0 .conc0303 .homework ;
22
3+ import java .util .concurrent .Semaphore ;
4+
35/**
4- * 应用 volatile 机制
6+ * 应用Semaphore机制
57 */
68public class AsyncResultImpl8 implements AsyncResult {
79 private volatile int result = -1 ;
8- private volatile boolean hadComputed = false ;
10+
911 @ Override
1012 public int getResult () throws Exception {
13+ final Semaphore semaphore = new Semaphore (2 );
14+ semaphore .acquire (1 );
1115 new Thread (new Runnable () {
1216 @ Override
1317 public void run () {
14- result = sum ();
15- hadComputed = true ;
18+ try {
19+ result = sum ();
20+ }finally {
21+ semaphore .release (1 );
22+ }
1623 }
1724 }).start ();
1825
19- while (!hadComputed ){
20- Thread .yield ();// 防止一直占用线程
21- }
26+ semaphore .acquire (2 );
2227 return result ;
2328 }
2429}
You can’t perform that action at this time.
0 commit comments