Skip to content

Commit 1240199

Browse files
author
cicadasmile
committed
Java并发:多线程并发访问,同步控制
1 parent 9d23ce3 commit 1240199

File tree

6 files changed

+198
-25
lines changed

6 files changed

+198
-25
lines changed

README.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
## 参考文章
22

3-
### JavaSE基础篇
3+
### JavaSE基础
44

5-
[Java基础篇(01):基本数据类型,核心点整理](https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484444&idx=2&sn=c5590ea2d71a0a358d2f9cd96c3c2b19&chksm=fdf450a4ca83d9b2383032c1aa354a59a0ca6d52981f2a58d6d3142bde7ed1605b4fcde3b4d9&token=1530600379&lang=zh_CN#rd)<br/>
6-
[Java基础篇(02):特殊的String类,和相关扩展API](https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484444&idx=1&sn=2cd50529d3d6ff97a9efce6158891c7c&chksm=fdf450a4ca83d9b29f3edf3c4999885278f7e9130a71b1d2f7f54057dcb07a3b95964547e862&token=1530600379&lang=zh_CN#rd)<br/>
7-
8-
### JavaSE并发篇
9-
10-
[Java并发编程(01):线程的创建方式,状态周期管理](https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484431&idx=1&sn=9bdac46fa8309a7618542471c8256b8f&chksm=fdf450b7ca83d9a17a3615c5380a163b93b68a7f2c506e533eec4921b87e8386c65dd629a159&token=1530600379&lang=zh_CN#rd)<br/>
5+
|序号|文章标题|
6+
|:---:|:---|
7+
|A01|[Java基础:基本数据类型,核心点整理](https://mp.weixin.qq.com/s/YwrBptAd2SrWefFIiN86Yw)|
8+
|A02|[Java基础:特殊的String类,和相关扩展API](https://mp.weixin.qq.com/s/HI6yklzR118G2Y-rJzl47g)|
9+
|B01|[Java并发:线程的创建方式,状态周期管理](https://mp.weixin.qq.com/s/h0zuh364It-fDtQyYdzIwA)|
10+
|B02|[Java并发:线程核心机制,基础概念扩展](https://mp.weixin.qq.com/s/nU5tbKuAiDf_LDu-8A4Etg)|
11+
|B03|[Java并发:多线程并发访问,同步控制](https://mp.weixin.qq.com/s/xWhd3vgYPNp-Mm2lJpmFmA)|
1112

1213
### JavaEE基础
1314

14-
[JavaEE基础(01):Servlet实现方式,生命周期执行过程](https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484304&idx=1&sn=dd6b6852e35031dd07f70d441f3ddc85&chksm=fdf45728ca83de3e158597030cf46b1677eccf533f9e1412690cd64b0ec0cce544711ceabddb&token=1248678182&lang=zh_CN#rd)<br/>
15-
[JavaEE基础(02):Servlet核心API用法详解](https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484309&idx=1&sn=fc8237aef2f246f85b978561f4b37e1e&chksm=fdf4572dca83de3b8affdf4a9a8c8b3c18e4c5203c3e663e0bfaeb53788d3e2d26fa829bb8c6&token=1248678182&lang=zh_CN#rd)<br/>
16-
[JavaEE基础(03):Http请求详解,握手挥手流程简介](https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484318&idx=1&sn=125780e20ada2f3d1451a1563a1c1e6f&chksm=fdf45726ca83de30adc5956a16d16151825b37c0adf5350214899ade39fef47f17ee35b2390d&token=1248678182&lang=zh_CN#rd)<br/>
17-
[JavaEE基础(04):会话跟踪技术,Session和Cookie详解](https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484325&idx=1&sn=a3c96cff45fac355d947dff9e7f5b9bf&chksm=fdf4571dca83de0b590bbc2864561c33b4b7af512d5b0dce86eca83170a23f007a1786d22f8c&token=362281359&lang=zh_CN#rd)<br/>
18-
[JavaEE基础(05):过滤器、监听器、拦截器,应用详解](https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484330&idx=1&sn=2754da84764ba5db8bccb1112881af3e&chksm=fdf45712ca83de04848d00451ce473275514ec8e60786c6f8db355190b0360463eb10da6c94c&token=362281359&lang=zh_CN#rd)<br/>
19-
[JavaEE基础(06):Servlet整合C3P0数据库连接池](https://mp.weixin.qq.com/s?__biz=MzU4Njg0MzYwNw==&mid=2247484335&idx=1&sn=6755f997766ceb8b81c83201ba101553&chksm=fdf45717ca83de01b39de0f3e5ca15607d12960cf5f9e2df391bc6c6af0808018dd2b1969a73&token=362281359&lang=zh_CN#rd)<br/>
20-
15+
|序号|文章标题|
16+
|:---:|:---|
17+
|01|[Servlet实现方式,生命周期执行过程](https://mp.weixin.qq.com/s/0YMhMX4ET1BxyGJi01grnw)|
18+
|02|[Servlet核心API用法详解](https://mp.weixin.qq.com/s/gC9xknmXLvC7WlwEpPSWXQ)|
19+
|03|[Http请求详解,握手挥手流程简介](https://mp.weixin.qq.com/s/IhrIdiLu2kxe2npgix8KTg)|
20+
|04|[会话跟踪技术,Session和Cookie详解](https://mp.weixin.qq.com/s/8cEgI49Tbb4ezxZEvEq8tg)|
21+
|05|[过滤器、监听器、拦截器,应用详解](https://mp.weixin.qq.com/s/CVhLyZ12BbT9kaclSpIrnw)|
22+
|06|[Servlet整合C3P0数据库连接池](https://mp.weixin.qq.com/s/I-KB6vAEwVMBAVAjEP2RmQ)|
2123
持续更新中...
2224

2325
## 项目简介
@@ -30,14 +32,12 @@ Java基础类型,容器,并发,IO流,面向对象,Web编程等代码
3032

3133
## 推荐项目
3234

33-
|项目名称|GitHub地址|GitEE地址|推荐指数|
34-
|:---|:---|:---|:---|
35-
|Java描述常用设计模式,算法,数据结构|[GitHub·点这里](https://github.com/cicadasmile/model-arithmetic-parent)|[GitEE·点这里](https://gitee.com/cicadasmile/model-arithmetic-parent)|☆☆☆☆☆|
36-
|Java基础、进阶、并发、面向对象、Web开发|[GitHub·点这里](https://github.com/cicadasmile/java-base-parent)|[GitEE·点这里](https://gitee.com/cicadasmile/java-base-parent)|☆☆☆☆|
37-
|SpringCloud微服务架构实战综合案例|[GitHub·点这里](https://github.com/cicadasmile/husky-spring-cloud)|[GitEE·点这里](https://gitee.com/cicadasmile/husky-spring-cloud)|☆☆☆☆☆|
38-
|SpringCloud微服务基础组件案例详解|[GitHub·点这里](https://github.com/cicadasmile/spring-cloud-base)|[GitEE·点这里](https://gitee.com/cicadasmile/spring-cloud-base)|☆☆☆|
39-
|SpringCloud实现分库分表模式下数据库实时扩容|[GitHub·点这里](https://github.com/cicadasmile/cloud-shard-jdbc)|[GitEE·点这里](https://gitee.com/cicadasmile/cloud-shard-jdbc)|☆☆☆☆☆|
40-
|SpringBoot框架基础应用入门到进阶|[GitHub·点这里](https://github.com/cicadasmile/spring-boot-base)|[GitEE·点这里](https://gitee.com/cicadasmile/spring-boot-base)|☆☆☆☆|
41-
|SpringBoot框架整合开发常用中间件|[GitHub·点这里](https://github.com/cicadasmile/middle-ware-parent)|[GitEE·点这里](https://gitee.com/cicadasmile/middle-ware-parent)|☆☆☆☆☆|
42-
|Spring+Mvc框架基础案例详解|[GitHub·点这里](https://github.com/cicadasmile/spring-mvc-parent)|[GitEE·点这里](https://gitee.com/cicadasmile/spring-mvc-parent)|☆☆|
43-
|Linux系统基础、运维,常用操作积累|[GitHub·点这里](https://github.com/cicadasmile/linux-system-base)|[GitEE·点这里](https://gitee.com/cicadasmile/linux-system-base)|☆☆☆|
35+
|序号|项目名称|GitHub地址|GitEE地址|推荐指数|
36+
|:---|:---|:---|:---|:---|
37+
|01|Java描述设计模式,算法,数据结构|[GitHub·点这里](https://github.com/cicadasmile/model-arithmetic-parent)|[GitEE·点这里](https://gitee.com/cicadasmile/model-arithmetic-parent)|☆☆☆☆☆|
38+
|02|Java基础、并发、面向对象、Web开发|[GitHub·点这里](https://github.com/cicadasmile/java-base-parent)|[GitEE·点这里](https://gitee.com/cicadasmile/java-base-parent)|☆☆☆☆|
39+
|03|SpringCloud微服务基础组件案例详解|[GitHub·点这里](https://github.com/cicadasmile/spring-cloud-base)|[GitEE·点这里](https://gitee.com/cicadasmile/spring-cloud-base)|☆☆☆|
40+
|04|SpringCloud微服务架构实战综合案例|[GitHub·点这里](https://github.com/cicadasmile/husky-spring-cloud)|[GitEE·点这里](https://gitee.com/cicadasmile/husky-spring-cloud)|☆☆☆☆☆|
41+
|05|SpringBoot框架基础应用入门到进阶|[GitHub·点这里](https://github.com/cicadasmile/spring-boot-base)|[GitEE·点这里](https://gitee.com/cicadasmile/spring-boot-base)|☆☆☆☆|
42+
|06|SpringBoot框架整合开发常用中间件|[GitHub·点这里](https://github.com/cicadasmile/middle-ware-parent)|[GitEE·点这里](https://gitee.com/cicadasmile/middle-ware-parent)|☆☆☆☆☆|
43+
|07|数据管理、分布式、架构设计基础案例|[GitHub·点这里](https://github.com/cicadasmile/data-manage-parent)|[GitEE·点这里](https://gitee.com/cicadasmile/data-manage-parent)|☆☆☆☆☆|
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.multy.thread.block03access;
2+
3+
public class AccessVar01 {
4+
public static void main(String[] args) {
5+
Var01Test var01Test = new Var01Test() ;
6+
VarThread01A varThread01A = new VarThread01A(var01Test) ;
7+
varThread01A.start();
8+
VarThread01B varThread01B = new VarThread01B(var01Test) ;
9+
varThread01B.start();
10+
}
11+
}
12+
class VarThread01A extends Thread {
13+
Var01Test var01Test = new Var01Test() ;
14+
public VarThread01A (Var01Test var01Test){
15+
this.var01Test = var01Test ;
16+
}
17+
@Override
18+
public void run() {
19+
var01Test.addNum(50);
20+
}
21+
}
22+
class VarThread01B extends Thread {
23+
Var01Test var01Test = new Var01Test() ;
24+
public VarThread01B (Var01Test var01Test){
25+
this.var01Test = var01Test ;
26+
}
27+
@Override
28+
public void run() {
29+
var01Test.addNum(10);
30+
}
31+
}
32+
class Var01Test {
33+
private Integer num = 0 ;
34+
public synchronized void addNum (Integer var){
35+
// Integer num = 0 ;
36+
try {
37+
if (var == 50){
38+
num = num + 50 ;
39+
Thread.sleep(3000);
40+
} else {
41+
num = num + var ;
42+
}
43+
System.out.println("var="+var+";num="+num);
44+
} catch (Exception e){
45+
e.printStackTrace();
46+
}
47+
}
48+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.multy.thread.block03access;
2+
3+
public class AccessVar02 {
4+
public static void main(String[] args) {
5+
Var02Test var02Test = new Var02Test ();
6+
VarThread02A varThread02A = new VarThread02A(var02Test) ;
7+
varThread02A.start();
8+
VarThread02B varThread02B = new VarThread02B(var02Test) ;
9+
varThread02B.start();
10+
var02Test.readValue();
11+
}
12+
}
13+
class VarThread02A extends Thread {
14+
Var02Test var02Test = new Var02Test ();
15+
public VarThread02A (Var02Test var02Test){
16+
this.var02Test = var02Test ;
17+
}
18+
@Override
19+
public void run() {
20+
var02Test.change("my","name");
21+
}
22+
}
23+
class VarThread02B extends Thread {
24+
Var02Test var02Test = new Var02Test ();
25+
public VarThread02B (Var02Test var02Test){
26+
this.var02Test = var02Test ;
27+
}
28+
@Override
29+
public void run() {
30+
var02Test.change("you","age");
31+
}
32+
}
33+
class Var02Test {
34+
public String key = "cicada" ;
35+
public String value = "smile" ;
36+
public synchronized void change (String key,String value){
37+
try {
38+
this.key = key ;
39+
Thread.sleep(2000);
40+
this.value = value ;
41+
System.out.println("key="+key+";value="+value);
42+
} catch (InterruptedException e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
public synchronized void readValue (){
47+
System.out.println("读取:key="+key+";value="+value);
48+
}
49+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.multy.thread.block03access;
2+
3+
public class AccessVar03 {
4+
public static void main(String[] args) {
5+
Var03Test var03Test1 = new Var03Test() ;
6+
Thread thread1 = new Thread(var03Test1) ;
7+
thread1.start();
8+
Thread thread2 = new Thread(var03Test1) ;
9+
thread2.start();
10+
Thread thread3 = new Thread(var03Test1) ;
11+
thread3.start();
12+
}
13+
}
14+
class Var03Test implements Runnable {
15+
private Integer count = 0 ;
16+
public void countAdd() {
17+
try {
18+
Thread.sleep(1000);
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
synchronized(this) {
23+
count++ ;
24+
System.out.println("count="+count);
25+
}
26+
}
27+
@Override
28+
public void run() {
29+
countAdd() ;
30+
}
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.multy.thread.block03access;
2+
3+
public class AccessVar04 {
4+
public static void main(String[] args) {
5+
Var04Test var04Test1 = new Var04Test() ;
6+
Thread thread1 = new Thread(var04Test1) ;
7+
thread1.start();
8+
Var04Test var04Test2 = new Var04Test() ;
9+
Thread thread2 = new Thread(var04Test2) ;
10+
thread2.start();
11+
}
12+
}
13+
class Var04Test implements Runnable {
14+
private static Integer count ;
15+
public Var04Test (){
16+
count = 0 ;
17+
}
18+
public synchronized static void countAdd() {
19+
System.out.println(Thread.currentThread().getName()+";count="+(count++));
20+
}
21+
@Override
22+
public void run() {
23+
countAdd() ;
24+
}
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.multy.thread.block03access;
2+
3+
public class AccessVar05 {
4+
}
5+
class Var05Test {
6+
private volatile boolean myFlag = true ;
7+
public void setFlag (boolean myFlag){
8+
this.myFlag = myFlag ;
9+
}
10+
public void method() {
11+
while (myFlag){
12+
try {
13+
System.out.println(Thread.currentThread().getName()+myFlag);
14+
Thread.sleep(1000);
15+
} catch (InterruptedException e) {
16+
e.printStackTrace();
17+
}
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)