File tree Expand file tree Collapse file tree
src/main/java/com/chen/algorithm/znn/thread Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .chen .algorithm .znn .thread ;
2+
3+ import java .util .concurrent .ExecutorService ;
4+ import java .util .concurrent .Executors ;
5+
6+ /**
7+ * @Auther: zhunn
8+ * @Date: 2021/1/26 13:47
9+ * @Description: 仅使用volatile关键字
10+ */
11+ public class ThreadTest {
12+
13+ private volatile static int count = 0 ;
14+
15+ private volatile static boolean flag = true ;
16+
17+
18+ public static void main (String [] args ) {
19+
20+ // 启用线程池
21+ ExecutorService executorService = Executors .newCachedThreadPool ();
22+
23+ // 先从0,偶数开始
24+ executorService .execute (() -> {
25+ while (count <= 100 ) {
26+ if (flag ) {
27+ System .out .println ("偶数:" + count ++);
28+ flag = false ;
29+ }
30+ }
31+ });
32+ executorService .execute (() -> {
33+ while (count <= 100 ) {
34+ if (!flag ) {
35+ System .out .println ("奇数:" + count ++);
36+ flag = true ;
37+ }
38+ }
39+ });
40+ executorService .shutdown ();
41+ }
42+
43+ }
Original file line number Diff line number Diff line change 1+ package com .chen .algorithm .znn .thread ;
2+
3+ import java .util .concurrent .ExecutorService ;
4+ import java .util .concurrent .Executors ;
5+ import java .util .concurrent .atomic .AtomicInteger ;
6+
7+ /**
8+ * @Auther: zhunn
9+ * @Date: 2021/1/26 13:54
10+ * @Description: 使用AtomicInteger
11+ */
12+ public class ThreadTest2 {
13+ private static AtomicInteger count = new AtomicInteger (0 );
14+
15+ private volatile static boolean flag = true ;
16+
17+ public static void main (String [] args ) {
18+
19+ // 启用线程池
20+ ExecutorService executorService = Executors .newCachedThreadPool ();
21+
22+ // 先从0,偶数开始
23+ executorService .execute (() -> {
24+ while (count .get () <= 100 ) {
25+ if (flag ) {
26+ System .out .println ("偶数:" + count .getAndIncrement ());
27+ flag = false ;
28+ }
29+ }
30+ });
31+ executorService .execute (() -> {
32+ while (count .get () <= 100 ) {
33+ if (!flag ) {
34+ System .out .println ("奇数:" + count .getAndIncrement ());
35+ flag = true ;
36+ }
37+ }
38+ });
39+ executorService .shutdown ();
40+ }
41+
42+ }
Original file line number Diff line number Diff line change 1+ package com .chen .algorithm .znn .thread ;
2+
3+ import java .util .concurrent .ExecutorService ;
4+ import java .util .concurrent .Executors ;
5+
6+ /**
7+ * @Auther: zhunn
8+ * @Date: 2021/1/26 13:55
9+ * @Description: 使用synchronized
10+ */
11+ public class ThreadTest3 {
12+ // 需要打印的资源: 0~100
13+ private static int count = 0 ;
14+
15+ private static Object lock = new Object ();
16+
17+ public static void main (String [] args ) {
18+
19+ // 启用线程池
20+ ExecutorService executorService = Executors .newCachedThreadPool ();
21+
22+ // 先从0,偶数开始
23+ executorService .execute (() -> {
24+ while (count <= 100 ) {
25+ synchronized (lock ) {
26+ try {
27+ System .out .println ("偶: " + count );
28+ count ++;
29+ } catch (Exception e ) {
30+ e .printStackTrace ();
31+ } finally {
32+ // 释放奇数线程
33+ lock .notifyAll ();
34+ if (count <= 100 ) {
35+ try {
36+ // 防止偶数线程继续执行
37+ lock .wait ();
38+ } catch (InterruptedException e ) {
39+ e .printStackTrace ();
40+ }
41+ }
42+ }
43+ }
44+ }
45+ });
46+ executorService .execute (() -> {
47+ while (count <= 100 ) {
48+ synchronized (lock ) {
49+ try {
50+ System .out .println ("奇: " + count );
51+ count ++;
52+ } catch (Exception e ) {
53+ e .printStackTrace ();
54+ } finally {
55+ lock .notifyAll ();
56+ if (count <= 100 ) {
57+ try {
58+ lock .wait ();
59+ } catch (InterruptedException e ) {
60+ e .printStackTrace ();
61+ }
62+ }
63+ }
64+ }
65+ }
66+ });
67+ executorService .shutdown ();
68+ }
69+
70+ }
Original file line number Diff line number Diff line change 1+ package com .chen .algorithm .znn .thread ;
2+
3+ import java .util .concurrent .ExecutorService ;
4+ import java .util .concurrent .Executors ;
5+ import java .util .concurrent .locks .Condition ;
6+ import java .util .concurrent .locks .ReentrantLock ;
7+
8+ /**
9+ * @Auther: zhunn
10+ * @Date: 2021/1/26 13:55
11+ * @Description: 使用ReentrantLock与Condition
12+ */
13+ public class ThreadTest4 {
14+ // 需要打印的资源: 0~100
15+ private static int count = 0 ;
16+
17+ // 通过condition控制线程竞争
18+ private static final ReentrantLock lock = new ReentrantLock ();
19+ private static final Condition condition1 = lock .newCondition ();
20+ private static final Condition condition2 = lock .newCondition ();
21+
22+ public static void main (String [] args ) {
23+
24+ // 启用线程池
25+ ExecutorService executorService = Executors .newCachedThreadPool ();
26+
27+ // 先从0,偶数开始
28+ executorService .execute (() -> {
29+ while (count <= 100 ) {
30+ try {
31+ lock .lock ();
32+ System .out .println ("偶: " + count );
33+ count ++;
34+ // 把偶数线程阻塞
35+ condition1 .await ();
36+ // 把奇数线程唤醒
37+ condition2 .signal ();
38+ } catch (InterruptedException e ) {
39+ e .printStackTrace ();
40+ } finally {
41+ lock .unlock ();
42+ }
43+ }
44+ });
45+ executorService .execute (() -> {
46+ while (count <= 100 ) {
47+ try {
48+ lock .lock ();
49+ System .out .println ("奇: " + count );
50+ count ++;
51+ // 把偶数线程唤醒
52+ condition1 .signal ();
53+ // 把奇数线程阻塞
54+ condition2 .await ();
55+ } catch (InterruptedException e ) {
56+ e .printStackTrace ();
57+ } finally {
58+ lock .unlock ();
59+ }
60+ }
61+ });
62+ executorService .shutdown ();
63+ }
64+
65+ }
Original file line number Diff line number Diff line change 1+ package com .chen .algorithm .znn .thread ;
2+
3+ import java .util .concurrent .ExecutorService ;
4+ import java .util .concurrent .Executors ;
5+ import java .util .concurrent .locks .ReentrantLock ;
6+
7+ /**
8+ * @Auther: zhunn
9+ * @Date: 2021/1/26 13:56
10+ * @Description: 仅使用ReentrantLock
11+ */
12+ public class ThreadTest5 {
13+ // 需要打印的资源: 0~100
14+ private static int count = 0 ;
15+
16+ // 是否执行打印的标志
17+ private static volatile boolean flag = false ;
18+
19+ // 通过加锁控制线程竞争
20+ private static final ReentrantLock lock = new ReentrantLock ();
21+
22+ public static void main (String [] args ) {
23+
24+ // 启用线程池
25+ ExecutorService executorService = Executors .newCachedThreadPool ();
26+
27+ // 先从0,偶数开始
28+ executorService .execute (() -> {
29+ while (count <= 100 ) {
30+ if (!flag ) {
31+ try {
32+ lock .lock ();
33+ System .out .println ("偶: " + count );
34+ count ++;
35+ // 只有flag变为true了,此线程才不会接着执行,将争夺权给奇数线程
36+ flag = true ;
37+ } finally {
38+ lock .unlock ();
39+ }
40+ } else {
41+ // 防止线程空转
42+ try {
43+ Thread .sleep (10 );
44+ } catch (InterruptedException e ) {
45+ e .printStackTrace ();
46+ }
47+ }
48+ }
49+ });
50+ executorService .execute (() -> {
51+ while (count <= 100 ) {
52+ // 当偶数线程执行后flag变为true,此线程可以执行了
53+ if (flag ) {
54+ try {
55+ lock .lock ();
56+ System .out .println ("奇: " + count );
57+ count ++;
58+ // 让出CPU
59+ flag = false ;
60+ } finally {
61+ lock .unlock ();
62+ }
63+ } else {
64+ try {
65+ Thread .sleep (10 );
66+ } catch (InterruptedException e ) {
67+ e .printStackTrace ();
68+ }
69+ }
70+ }
71+ });
72+ executorService .shutdown ();
73+ }
74+
75+ }
You can’t perform that action at this time.
0 commit comments