Skip to content

Commit 8196fd0

Browse files
author
chenweijie
committed
testMutex
1 parent 466967f commit 8196fd0

File tree

7 files changed

+172
-4
lines changed

7 files changed

+172
-4
lines changed

src/main/java/com/chen/api/util/aqs/Mutex.java renamed to src/main/java/com/chen/api/util/lock/aqs/Mutex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.chen.api.util.aqs;
1+
package com.chen.api.util.lock.aqs;
22

33
import java.util.concurrent.TimeUnit;
44
import java.util.concurrent.locks.AbstractQueuedSynchronizer;

src/main/java/com/chen/api/util/aqs/TwinsLock.java renamed to src/main/java/com/chen/api/util/lock/aqs/TwinsLock.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.chen.api.util.aqs;
1+
package com.chen.api.util.lock.aqs;
22

33
import java.util.concurrent.TimeUnit;
44
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
@@ -25,6 +25,7 @@ private static final class Sync extends AbstractQueuedSynchronizer {
2525
setState(count);
2626
}
2727

28+
@Override
2829
public int tryAcquireShared(int reduceCount) {
2930
for (; ; ) {
3031
int current = getState();
@@ -36,6 +37,7 @@ public int tryAcquireShared(int reduceCount) {
3637
}
3738

3839

40+
@Override
3941
public boolean tryReleaseShared(int returnCount) {
4042
for (; ; ) {
4143
int current = getState();

src/main/java/com/chen/api/util/aqs/TwinsLockTest.java renamed to src/main/java/com/chen/api/util/lock/aqs/TwinsLockTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.chen.api.util.aqs;
1+
package com.chen.api.util.lock.aqs;
22

33
import java.util.concurrent.locks.Lock;
44

src/main/java/com/chen/api/util/thread/mutex/TestMutex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.chen.api.util.thread.mutex;
22

33

4-
import com.chen.api.util.aqs.Mutex;
4+
import com.chen.api.util.lock.aqs.Mutex;
55

66
import java.util.concurrent.BrokenBarrierException;
77
import java.util.concurrent.CyclicBarrier;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

0 commit comments

Comments
 (0)