File tree 3 files changed +195
-0
lines changed
00-code(源代码)/src/com/hi/dhl/algorithms/other/concurrency/_1116
3 files changed +195
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .hi .dhl .algorithms .other .concurrency ._1116 ;
2
+
3
+ import java .util .concurrent .locks .Condition ;
4
+ import java .util .concurrent .locks .Lock ;
5
+ import java .util .concurrent .locks .ReentrantLock ;
6
+ import java .util .function .IntConsumer ;
7
+
8
+ /**
9
+ * <pre>
10
+ * author: dhl
11
+ * date : 2020/10/31
12
+ * desc :
13
+ * </pre>
14
+ */
15
+
16
+ class ZeroEvenOddForCondition {
17
+ private int n ;
18
+ private Lock lock = new ReentrantLock ();
19
+ private Condition czero = lock .newCondition ();
20
+ private Condition ceven = lock .newCondition ();
21
+ private Condition codd = lock .newCondition ();
22
+ private int value = 0 ;
23
+ private boolean zero = true ;
24
+
25
+ public ZeroEvenOddForCondition (int n ) {
26
+ this .n = n ;
27
+ }
28
+
29
+ // printNumber.accept(x) outputs "x", where x is an integer.
30
+ public void zero (IntConsumer printNumber ) throws InterruptedException {
31
+ lock .lock ();
32
+ try {
33
+ for (int i = 1 ; i <= n ; i ++) {
34
+ while (!zero ) {
35
+ czero .await ();
36
+ }
37
+ printNumber .accept (0 );
38
+ zero = false ;
39
+ value ++;
40
+ if ((i & 1 ) == 1 ) {
41
+ codd .signalAll ();
42
+ } else {
43
+ ceven .signalAll ();
44
+ }
45
+ }
46
+ } finally {
47
+ lock .unlock ();
48
+ }
49
+ }
50
+
51
+ public void even (IntConsumer printNumber ) throws InterruptedException {
52
+ lock .lock ();
53
+ try {
54
+ for (int i = 2 ; i <= n ; i += 2 ) {
55
+ while (zero || (value & 1 ) == 1 ) {
56
+ ceven .await ();
57
+ }
58
+ printNumber .accept (i );
59
+ zero = true ;
60
+ czero .signalAll ();
61
+ }
62
+ } finally {
63
+ lock .unlock ();
64
+ }
65
+ }
66
+
67
+ public void odd (IntConsumer printNumber ) throws InterruptedException {
68
+ lock .lock ();
69
+ try {
70
+ for (int i = 1 ; i <= n ; i += 2 ) {
71
+ while (zero || (value & 1 ) != 1 ) {
72
+ codd .await ();
73
+ }
74
+ printNumber .accept (i );
75
+ zero = true ;
76
+ czero .signalAll ();
77
+ }
78
+ } finally {
79
+ lock .unlock ();
80
+ }
81
+ }
82
+ }
Original file line number Diff line number Diff line change
1
+ package com .hi .dhl .algorithms .other .concurrency ._1116 ;
2
+
3
+ import java .util .concurrent .Semaphore ;
4
+ import java .util .function .IntConsumer ;
5
+
6
+ /**
7
+ * <pre>
8
+ * author: dhl
9
+ * date : 2020/10/31
10
+ * desc :
11
+ * </pre>
12
+ */
13
+ class ZeroEvenOddForSemaphore {
14
+ private int n ;
15
+ private Semaphore szero = new Semaphore (1 );
16
+ private Semaphore seven = new Semaphore (0 );
17
+ private Semaphore sodd = new Semaphore (0 );
18
+
19
+ public ZeroEvenOddForSemaphore (int n ) {
20
+ this .n = n ;
21
+ }
22
+
23
+ // printNumber.accept(x) outputs "x", where x is an integer.
24
+ public void zero (IntConsumer printNumber ) throws InterruptedException {
25
+ for (int i =1 ;i <=n ;i ++){
26
+ szero .acquire ();
27
+ printNumber .accept (0 );
28
+ if ((i & 1 ) == 1 ){
29
+ sodd .release ();
30
+ }else {
31
+ seven .release ();
32
+ }
33
+ }
34
+ }
35
+
36
+ public void even (IntConsumer printNumber ) throws InterruptedException {
37
+ for (int i = 2 ; i <=n ; i +=2 ){
38
+ seven .acquire ();
39
+ printNumber .accept (i );
40
+ szero .release ();
41
+ }
42
+ }
43
+
44
+ public void odd (IntConsumer printNumber ) throws InterruptedException {
45
+ for (int i = 1 ; i <=n ; i +=2 ){
46
+ sodd .acquire ();
47
+ printNumber .accept (i );
48
+ szero .release ();
49
+ }
50
+ }
51
+ }
Original file line number Diff line number Diff line change
1
+ package com .hi .dhl .algorithms .other .concurrency ._1116 ;
2
+
3
+ import java .util .function .IntConsumer ;
4
+
5
+ /**
6
+ * <pre>
7
+ * author: dhl
8
+ * date : 2020/10/31
9
+ * desc :
10
+ * </pre>
11
+ */
12
+ class ZeroEvenOddForWait {
13
+ private int n ;
14
+ private final Object LOCK = new Object ();
15
+ private boolean zero = true ;
16
+ private int value = 0 ;
17
+
18
+ public ZeroEvenOddForWait (int n ) {
19
+ this .n = n ;
20
+ }
21
+
22
+ // printNumber.accept(x) outputs "x", where x is an integer.
23
+ public void zero (IntConsumer printNumber ) throws InterruptedException {
24
+ for (int i = 1 ; i <= n ; i ++) {
25
+ synchronized (LOCK ) {
26
+ while (!zero ) {
27
+ LOCK .wait ();
28
+ }
29
+ printNumber .accept (0 );
30
+ zero = false ;
31
+ value ++;
32
+ LOCK .notifyAll ();
33
+ }
34
+ }
35
+ }
36
+
37
+ public void even (IntConsumer printNumber ) throws InterruptedException {
38
+ for (int i = 2 ; i <= n ; i += 2 ) {
39
+ synchronized (LOCK ) {
40
+ while (zero || (value & 1 ) == 1 ) {
41
+ LOCK .wait ();
42
+ }
43
+ printNumber .accept (i );
44
+ zero = true ;
45
+ LOCK .notifyAll ();
46
+ }
47
+ }
48
+ }
49
+
50
+ public void odd (IntConsumer printNumber ) throws InterruptedException {
51
+ for (int i = 1 ; i <= n ; i += 2 ) {
52
+ synchronized (LOCK ) {
53
+ while (zero || (value & 1 ) != 1 ) {
54
+ LOCK .wait ();
55
+ }
56
+ printNumber .accept (i );
57
+ zero = true ;
58
+ LOCK .notifyAll ();
59
+ }
60
+ }
61
+ }
62
+ }
You can’t perform that action at this time.
0 commit comments