Skip to content

Commit 2daa30a

Browse files
author
deleiguo
committed
update
1 parent 3a7cc41 commit 2daa30a

File tree

5 files changed

+246
-5
lines changed

5 files changed

+246
-5
lines changed

source/java/interview/delei-interview-common/pom.xml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45
<artifactId>delei-interview-common</artifactId>
56
<packaging>jar</packaging>
@@ -25,12 +26,18 @@
2526
<dependency>
2627
<groupId>cn.hutool</groupId>
2728
<artifactId>hutool-all</artifactId>
28-
<version>5.7.18</version>
29+
<version>${util.hutools.version}</version>
2930
</dependency>
3031
<dependency>
3132
<groupId>com.alibaba</groupId>
3233
<artifactId>fastjson</artifactId>
33-
<version>1.2.73</version>
34+
<version>${util.fastjson.version}</version>
3435
</dependency>
36+
37+
<!-- <dependency>-->
38+
<!-- <groupId>com.alibaba.fastjson2</groupId>-->
39+
<!-- <artifactId>fastjson2</artifactId>-->
40+
<!-- <version>${util.fastjson.version}</version>-->
41+
<!-- </dependency>-->
3542
</dependencies>
3643
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package cn.delei.java.concurrent;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
5+
import java.util.concurrent.locks.Condition;
6+
import java.util.concurrent.locks.Lock;
7+
8+
/**
9+
* 独占锁 Mutex 是一个自定义同步组件,它在同一时刻只允许一个线程占有锁
10+
* state: 0,1
11+
*
12+
* @author deleiguo
13+
*/
14+
public class CustomMutexLock implements Lock {
15+
16+
private class Sync extends AbstractQueuedSynchronizer {
17+
@Override
18+
protected boolean tryAcquire(int arg) {
19+
if (compareAndSetState(0, 1)) {
20+
setExclusiveOwnerThread(Thread.currentThread());
21+
return true;
22+
}
23+
return false;
24+
}
25+
26+
@Override
27+
protected boolean tryRelease(int arg) {
28+
if (getState() == 0) {
29+
throw new IllegalMonitorStateException();
30+
}
31+
setState(0);
32+
setExclusiveOwnerThread(null);
33+
return true;
34+
}
35+
36+
@Override
37+
protected boolean isHeldExclusively() {
38+
return getState() == 1;
39+
}
40+
41+
Condition newCondition() {
42+
return new ConditionObject();
43+
}
44+
45+
}
46+
47+
private final Sync sync = new Sync();
48+
49+
@Override
50+
public void lock() {
51+
sync.acquire(1);
52+
}
53+
54+
@Override
55+
public void lockInterruptibly() throws InterruptedException {
56+
sync.acquireInterruptibly(1);
57+
}
58+
59+
@Override
60+
public boolean tryLock() {
61+
return sync.tryAcquire(1);
62+
}
63+
64+
@Override
65+
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
66+
return sync.tryAcquireNanos(1, unit.toNanos(time));
67+
}
68+
69+
@Override
70+
public void unlock() {
71+
sync.release(1);
72+
}
73+
74+
@Override
75+
public Condition newCondition() {
76+
return sync.newCondition();
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package cn.delei.java.concurrent;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
5+
import java.util.concurrent.locks.Condition;
6+
import java.util.concurrent.locks.Lock;
7+
8+
/**
9+
* 在同一时刻允许至多两个线程的同时访问,表明同步资源数为 2
10+
* state:0,1,2
11+
* <p>
12+
* 设置初始状态 status 为 2,当一个线程进行获取,status 减 1,该 线程释放,则 status 加 1
13+
*
14+
* @author deleiguo
15+
*/
16+
public class CustomTwinsLock implements Lock {
17+
18+
private class Sync extends AbstractQueuedSynchronizer {
19+
20+
public Sync(int count) {
21+
if (count < 1) {
22+
throw new IllegalArgumentException();
23+
}
24+
setState(count);
25+
}
26+
27+
@Override
28+
protected int tryAcquireShared(int arg) {
29+
for (; ; ) {
30+
int s = getState();
31+
int newCount = s - arg;
32+
if (newCount < 0 || compareAndSetState(s, newCount)) {
33+
return newCount;
34+
}
35+
}
36+
}
37+
38+
@Override
39+
protected boolean tryReleaseShared(int arg) {
40+
for (; ; ) {
41+
int s = getState();
42+
int newCount = s + arg;
43+
if (compareAndSetState(s, newCount)) {
44+
return true;
45+
}
46+
}
47+
}
48+
49+
Condition newCondition() {
50+
return new ConditionObject();
51+
}
52+
53+
}
54+
55+
private final Sync sync = new Sync(2);
56+
57+
@Override
58+
public void lock() {
59+
sync.acquireShared(1);
60+
}
61+
62+
@Override
63+
public void lockInterruptibly() throws InterruptedException {
64+
sync.acquireSharedInterruptibly(1);
65+
}
66+
67+
@Override
68+
public boolean tryLock() {
69+
return sync.tryReleaseShared(1);
70+
}
71+
72+
@Override
73+
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
74+
return sync.tryAcquireSharedNanos(1, unit.toNanos(time));
75+
}
76+
77+
@Override
78+
public void unlock() {
79+
sync.releaseShared(1);
80+
}
81+
82+
@Override
83+
public Condition newCondition() {
84+
return sync.newCondition();
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cn.delei.java.concurrent;
2+
3+
import cn.hutool.core.util.RandomUtil;
4+
5+
import java.util.concurrent.locks.ReadWriteLock;
6+
import java.util.concurrent.locks.ReentrantReadWriteLock;
7+
8+
/**
9+
* ReadWriteLock 读写锁 demo
10+
*
11+
* @author deleiguo
12+
*/
13+
public class ReadWriteLockDemo {
14+
15+
public static void main(String[] args) {
16+
Innder innder = new Innder();
17+
// 一共启动6个线程,3个读线程,3个写线程
18+
for (int i = 0; i < 3; i++) {
19+
//启动1个读线程
20+
new Thread(() -> {
21+
while (true) {
22+
innder.get();
23+
}
24+
}, "TR-" + i).start();
25+
//启动1个写线程
26+
new Thread(() -> {
27+
while (true) {
28+
innder.put(RandomUtil.randomString(2));
29+
}
30+
}, "TW-" + i).start();
31+
}
32+
}
33+
34+
35+
static class Innder {
36+
37+
// 模拟共享数据,只能有一个线程能写该数据,但可以有多个线程同时读该数据。
38+
private String data = null;
39+
40+
private final ReadWriteLock lock = new ReentrantReadWriteLock();
41+
42+
void get() {
43+
// 加读锁
44+
lock.readLock().lock();
45+
try {
46+
Thread.sleep(RandomUtil.randomLong(1000, 3000));
47+
System.out.println(Thread.currentThread().getName() + " #get :" + data);
48+
} catch (InterruptedException e) {
49+
e.printStackTrace();
50+
} finally {
51+
lock.readLock().unlock();
52+
}
53+
}
54+
55+
void put(String d) {
56+
// 加读锁
57+
lock.writeLock().lock();
58+
try {
59+
Thread.sleep(RandomUtil.randomLong(1000, 3000));
60+
this.data = d;
61+
System.out.println(Thread.currentThread().getName() + " #put :" + data);
62+
} catch (InterruptedException e) {
63+
e.printStackTrace();
64+
} finally {
65+
lock.writeLock().unlock();
66+
}
67+
}
68+
}
69+
70+
}

source/java/interview/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636

3737
<delei.interview.version>1.0.0</delei.interview.version>
3838

39-
<util.hutools.version>5.3.18</util.hutools.version>
40-
<util.fastjson.version>2.0.0</util.fastjson.version>
39+
<util.hutools.version>5.7.18</util.hutools.version>
40+
<util.fastjson.version>2.0.2</util.fastjson.version>
4141

4242
<spring.version>5.2.12.RELEASE</spring.version>
4343
<spring.boot.version>2.2.13.RELEASE</spring.boot.version>

0 commit comments

Comments
 (0)