File tree Expand file tree Collapse file tree 7 files changed +172
-4
lines changed Expand file tree Collapse file tree 7 files changed +172
-4
lines changed Original file line number Diff line number Diff line change 1
- package com .chen .api .util .aqs ;
1
+ package com .chen .api .util .lock . aqs ;
2
2
3
3
import java .util .concurrent .TimeUnit ;
4
4
import java .util .concurrent .locks .AbstractQueuedSynchronizer ;
Original file line number Diff line number Diff line change 1
- package com .chen .api .util .aqs ;
1
+ package com .chen .api .util .lock . aqs ;
2
2
3
3
import java .util .concurrent .TimeUnit ;
4
4
import java .util .concurrent .locks .AbstractQueuedSynchronizer ;
@@ -25,6 +25,7 @@ private static final class Sync extends AbstractQueuedSynchronizer {
25
25
setState (count );
26
26
}
27
27
28
+ @ Override
28
29
public int tryAcquireShared (int reduceCount ) {
29
30
for (; ; ) {
30
31
int current = getState ();
@@ -36,6 +37,7 @@ public int tryAcquireShared(int reduceCount) {
36
37
}
37
38
38
39
40
+ @ Override
39
41
public boolean tryReleaseShared (int returnCount ) {
40
42
for (; ; ) {
41
43
int current = getState ();
Original file line number Diff line number Diff line change 1
- package com .chen .api .util .aqs ;
1
+ package com .chen .api .util .lock . aqs ;
2
2
3
3
import java .util .concurrent .locks .Lock ;
4
4
Original file line number Diff line number Diff line change 1
1
package com .chen .api .util .thread .mutex ;
2
2
3
3
4
- import com .chen .api .util .aqs .Mutex ;
4
+ import com .chen .api .util .lock . aqs .Mutex ;
5
5
6
6
import java .util .concurrent .BrokenBarrierException ;
7
7
import java .util .concurrent .CyclicBarrier ;
Original file line number Diff line number Diff line change
1
+ package com .chen .test ;
2
+
3
+ import java .util .concurrent .locks .AbstractQueuedSynchronizer ;
4
+
5
+ /**
6
+ * @author Chen WeiJie
7
+ * @date 2020-04-23 16:50:07
8
+ **/
9
+ public class Mutex {
10
+
11
+
12
+ public static class Sync extends AbstractQueuedSynchronizer {
13
+
14
+
15
+ @ Override
16
+ protected boolean isHeldExclusively () {
17
+ return getState () == 1 ;
18
+ }
19
+
20
+
21
+ @ Override
22
+ protected boolean tryAcquire (int arg ) {
23
+
24
+ if (compareAndSetState (0 , 1 )) {
25
+ setExclusiveOwnerThread (Thread .currentThread ());
26
+ return true ;
27
+ }
28
+ return false ;
29
+ }
30
+
31
+ @ Override
32
+ protected boolean tryRelease (int arg ) {
33
+
34
+ if (getState () == 0 ) {
35
+ throw new IllegalMonitorStateException ();
36
+ }
37
+ setExclusiveOwnerThread (null );
38
+ setState (0 );
39
+ return true ;
40
+ }
41
+
42
+ }
43
+
44
+ private final Sync sync = new Sync ();
45
+
46
+ public void lock () {
47
+ sync .acquire (1 );
48
+ }
49
+
50
+
51
+ public boolean tryLock () {
52
+ return sync .tryAcquire (1 );
53
+ }
54
+
55
+ public void unlock () {
56
+ sync .release (1 );
57
+ }
58
+
59
+ public boolean isLocked () {
60
+ return sync .isHeldExclusively ();
61
+ }
62
+
63
+ }
Original file line number Diff line number Diff line change
1
+ package com .chen .test ;
2
+
3
+ import java .util .Date ;
4
+
5
+ /**
6
+ * @author Chen WeiJie
7
+ * @date 2020-04-27 15:22:48
8
+ **/
9
+ public class Test2 {
10
+
11
+
12
+ public static void main (String [] args ) {
13
+
14
+ Date d = new Date ();
15
+
16
+ Date d2 = d ;
17
+
18
+ System .out .println (d .compareTo (d2 ));
19
+
20
+
21
+
22
+
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ package com .chen .test ;
2
+
3
+ import java .util .concurrent .BrokenBarrierException ;
4
+ import java .util .concurrent .CyclicBarrier ;
5
+
6
+ /**
7
+ * @author Chen WeiJie
8
+ * @date 2020-04-23 17:02:02
9
+ **/
10
+ public class TestMutex {
11
+
12
+
13
+ private static CyclicBarrier barrier = new CyclicBarrier (31 );
14
+
15
+ private static int a = 0 ;
16
+
17
+ private static Mutex mutex = new Mutex ();
18
+
19
+
20
+ public static void main (String [] args ) throws BrokenBarrierException , InterruptedException {
21
+
22
+
23
+ for (int i = 0 ; i < 30 ; i ++) {
24
+
25
+ new Thread (() -> {
26
+ for (int j = 0 ; j < 10000 ; j ++) {
27
+ increment ();
28
+ }
29
+ try {
30
+ barrier .await ();
31
+ } catch (InterruptedException e ) {
32
+ e .printStackTrace ();
33
+ } catch (BrokenBarrierException e ) {
34
+ e .printStackTrace ();
35
+ }
36
+ }).start ();
37
+ }
38
+
39
+ barrier .await ();
40
+ System .out .println ("a===" + a );
41
+ barrier .reset ();
42
+ a = 0 ;
43
+
44
+ for (int i = 0 ; i < 30 ; i ++) {
45
+ new Thread (() -> {
46
+ for (int j = 0 ; j < 10000 ; j ++) {
47
+ increment2 ();
48
+ }
49
+ try {
50
+ barrier .await ();
51
+ } catch (InterruptedException e ) {
52
+ e .printStackTrace ();
53
+ } catch (BrokenBarrierException e ) {
54
+ e .printStackTrace ();
55
+ }
56
+ }).start ();
57
+ }
58
+
59
+ barrier .await ();
60
+ System .out .println ("a===" + a );
61
+
62
+
63
+ }
64
+
65
+
66
+ public static void increment () {
67
+
68
+ a ++;
69
+ }
70
+
71
+
72
+ public static void increment2 () {
73
+ mutex .lock ();
74
+ a ++;
75
+ mutex .unlock ();
76
+ }
77
+
78
+
79
+ }
You can’t perform that action at this time.
0 commit comments