Java并发编程之闭锁与栅栏的实现
作者:与李 时间:2022-01-09 15:00:12
一、前言
闭锁与栅栏是在多线程编程中的概念,因为在多线程中,我们不能控制线程的执行状态,所以给线程加锁,让其按照我们的想法有秩序的执行。
闭锁
CountDownLatch,实例化时需要传入一个int类型的数字(count),意为等待count个线程完成之后才能执行下一步动作。
如今天要做的事情是吃晚饭,再去散步。假设11个人相约晚饭后一起去散步,我们得等11个人全都吃完晚饭了才能出发去散步。简而言之就是做了才到达某一种状态。
栅栏
CyclicBarrier,实例化时需要传入一个int类型的数字(parties),意为等待parties个线程都准备就绪后才能执行自己的任务。
如今天要做的事情是吃晚饭,8个人约好一起去某餐厅吃饭,得等到人齐了才能去吃饭。简而言之就是到达某种状态后一起做。
二、实例
闭锁 CountDownLatch
package com.test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
public class Test {
public static void main(String[] args) {
CountDownLatch latch = new CountDownLatch(3);
// 模拟三个任务
List<String> jobs = new ArrayList<String>();
jobs.add("first");
jobs.add("second");
jobs.add("third");
// 循环执行任务
for (String job : jobs) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " : 进入run方法");
latch.countDown();
System.out.println(Thread.currentThread().getName() + " : 执行" + job);
}
}).start();
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
// 任务都执行完后才执行
System.out.println("回到main线程");
}
}
执行结果:
Thread-1 : 进入run方法
Thread-2 : 进入run方法
Thread-2 : 执行third
Thread-0 : 进入run方法
Thread-1 : 执行second
Thread-0 : 执行first
回到main线程
通过执行结果可看出,当所有线程都执行完后才能回到主线程继续执行后面的输出。
栅栏 CyclicBarrier
package com.test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class Test {
public static void main(String[] args) {
CyclicBarrier barrier = new CyclicBarrier(3);
// 模拟创建三个任务
List<String> jobs = new ArrayList<String>();
jobs.add("first");
jobs.add("second");
jobs.add("third");
//循环执行任务
for (String job : jobs) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " : 进入run方法");
try {
// 等待
barrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " : 执行" + job);
}
}).start();
}
}
}
执行结果:
Thread-1 : 进入run方法
Thread-2 : 进入run方法
Thread-0 : 进入run方法
Thread-0 : 执行first
Thread-1 : 执行second
Thread-2 : 执行third
通过执行结果可看出,当所有线程都执行都进入到run方法后,才能继续执行自己内部的方法。
来源:https://blog.csdn.net/qq_37171817/article/details/105789565
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
心动吗?正大光明的免费使用IntelliJ IDEA商业版
![](https://img.aspxhome.com/file/2023/6/59976_0s.jpg)
JAVA各种OOM代码示例与解决方法
![](https://img.aspxhome.com/file/2023/5/72555_0s.jpg)
Java结构型设计模式之享元模式示例详解
![](https://img.aspxhome.com/file/2023/9/66059_0s.png)
Mybatis初始化知识小结
![](https://img.aspxhome.com/file/2023/8/58708_0s.png)
SpringBoot使用protobuf格式的接口方式
![](https://img.aspxhome.com/file/2023/0/70950_0s.png)
浅谈SpringBoot中的@Conditional注解的使用
![](https://img.aspxhome.com/file/2023/8/61798_0s.png)
C#利用com操作excel释放进程的解决方法
java实现五子棋程序
![](https://img.aspxhome.com/file/2023/7/63387_0s.jpg)
Java日期与时间类原理解析
java 如何判断是否是26个英文字母
Java 把json对象转成map键值对的方法
详解Java实现LRU缓存
Java servlet、filter、listener、interceptor之间的区别和联系
使用ehcache三步搞定springboot缓存的方法示例
Java实现提取QSV文件视频内容
Spring的@Validation和javax包下的@Valid区别以及自定义校验注解
深入理解Java虚拟机体系结构
![](https://img.aspxhome.com/file/2023/6/62366_0s.png)
SpringMVC实现多文件上传
![](https://img.aspxhome.com/file/2023/9/67999_0s.jpg)
详解Java泛型及其应用
java 如何实现正确的删除集合中的元素
![](https://img.aspxhome.com/file/2023/0/67680_0s.jpg)