Skip to content

Commit bc846ac

Browse files
committed
JUC:Guarded suspension mode normal version
1 parent d475894 commit bc846ac

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

JUCModeAll/SynchronizedMode/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,33 @@
3636
<version>4.13.2</version>
3737
<scope>provided</scope>
3838
</dependency>
39+
40+
<!--hutool 工具包-->
41+
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
42+
<dependency>
43+
<groupId>cn.hutool</groupId>
44+
<artifactId>hutool-all</artifactId>
45+
<version>5.8.16</version>
46+
</dependency>
47+
48+
<!--阿里fastJson2-->
49+
<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
50+
<dependency>
51+
<groupId>com.alibaba.fastjson2</groupId>
52+
<artifactId>fastjson2</artifactId>
53+
<version>2.0.42</version>
54+
</dependency>
55+
56+
57+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
58+
<dependency>
59+
<groupId>org.apache.commons</groupId>
60+
<artifactId>commons-lang3</artifactId>
61+
<version>3.12.0</version>
62+
</dependency>
63+
64+
65+
3966
</dependencies>
4067

4168
<!--指定 jdk 编译版本-->

JUCModeAll/SynchronizedMode/src/main/java/FairyQin/HomeLove/同步模式之保护性暂停/GuardedSuspensionMode.java

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,70 @@
99
* @description :616 An unchanging God Qin_Love
1010
* @vesion 1.0.0
1111
* @CreateDate 2023-08-19 21:49:50
12-
* @Description JUC模式中的同步模式之保护性暂停
12+
* @Description JUC模式中的同步模式之保护性暂停(普通版本)
1313
**/
1414
@Slf4j
1515
public class GuardedSuspensionMode {
16+
/**
17+
* @description: 保护性暂停模式:该模式的作用是用来保证一个线程等待另一个线程的执行结果
18+
* @author: AngelXin
19+
* @date: 2024/3/28 17:30
20+
**/
21+
static class GuardedObject{
22+
private Object response;
23+
private final Object lock=new Object();
24+
/**
25+
* @description: 获取结果的方法,如果结果没有返回则等待,如果结果返回则返回结果
26+
* @author: AngelXin
27+
* @date: 2024/3/29 10:09
28+
* @param: []
29+
* @return: java.lang.Object
30+
**/
31+
public Object get() {
32+
synchronized(lock) {
33+
while (response == null) {
34+
try {
35+
lock.wait(); // wait会释放锁
36+
} catch (InterruptedException e) {
37+
log.error("线程中断异常");
38+
}
39+
}
40+
return response;
41+
}
42+
}
43+
/**
44+
* @description: 计算赋值
45+
* @author: AngelXin
46+
* @date: 2024/3/29 10:12
47+
* @param: [response]
48+
* @return: void
49+
**/
50+
public void complete(Object response){
51+
synchronized(lock){
52+
this.response=response;
53+
lock.notifyAll();
54+
}
55+
}
56+
}
57+
58+
/**
59+
* @description: 测试保护性暂停
60+
* @author: AngelXin
61+
* @date: 2024/3/29 10:14
62+
* @param: [args]
63+
* @return: void
64+
**/
65+
public static void main(String[] args){
66+
GuardedObject guardedObject=new GuardedObject();
67+
Thread getThread=new Thread(()->{
68+
log.info("等待计算");
69+
guardedObject.complete("10");
70+
log.info("计算结束");
71+
},"计算线程");
72+
getThread.start();
73+
// 主线程阻塞等待
74+
log.info("等待结果");
75+
Object result = guardedObject.get();
76+
log.info("结果为:{}",result);
77+
}
1678
}

JUCModeAll/SynchronizedMode/src/main/java/FairyQin/HomeLove/同步模式之保护性暂停/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
- 因为要等待另一方的结果,因此归类到同步模式
1111
----
1212
**关系图:**
13-
![image-20230218224421773](https://cdn.staticaly.com/gh/GhostQinMo/ImageBed@master/ImagesJUC/image-20230218224421773.png)
13+
![image-20230218224421773](https://angelxinnotesimages.oss-cn-hangzhou.aliyuncs.com/JUC/image-20230218224421773.png)

SPIstandard/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@
5353
</excludes>
5454
</configuration>
5555
</plugin>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-compiler-plugin</artifactId>
59+
<configuration>
60+
<source>16</source>
61+
<target>16</target>
62+
</configuration>
63+
</plugin>
5664
</plugins>
5765
</build>
5866

0 commit comments

Comments
 (0)