java CyclicBarrier的介绍和使用
一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point)。在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待,此时 CyclicBarrier 很有用。因为该 barrier 在释放等待线程后可以重用,所以称它为循环 的 barrier。
举例说明:银行要进行账目录入,以防一个录入出现录入错误,采用两人对同一账目同时录入,以达到校对的效果
伪代码如下:
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier; /**
* Created by lv xiao long on 2017/7/16.
*/
public class Work implements Runnable { private CyclicBarrier barrier;
private String msg; public Work(String msg, CyclicBarrier barrier){
this.msg=msg;
this.barrier=barrier;
}
public void run() {
while (true) {
System.out.print(msg);
try {
barrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
break;
}
}
}
import java.util.concurrent.CyclicBarrier; /**
* by lv xiao long
*
*/
public class App
{
public static void main( String[] args )
{
//这里设置公共屏障点为3,两个子线程都到达屏障点后,主线程在放行,进入下账目录入,所以这里设置屏障点数为3
CyclicBarrier cyclicBarrier=new CyclicBarrier();
Work work=new Work("工作人员A录入完成\n",cyclicBarrier);
Thread thread=new Thread(work);
thread.start();
work=new Work("工作人员B录入完成\n",cyclicBarrier);
thread=new Thread(work);
thread.start();
try {
cyclicBarrier.await();
System.out.println( "可以开始进行下一账目录入!\n" );
} catch (Exception e) {
e.printStackTrace();
}
}
}
这里CycliBarrer 还有一个高级用法api如下
/**
* Creates a new <tt>CyclicBarrier</tt> that will trip when the
* given number of parties (threads) are waiting upon it, and which
* will execute the given barrier action when the barrier is tripped,
* performed by the last thread entering the barrier.
*
* @param parties the number of threads that must invoke {@link #await}
* before the barrier is tripped
* @param barrierAction the command to execute when the barrier is
* tripped, or {@code null} if there is no action
* @throws IllegalArgumentException if {@code parties} is less than 1
*/
public CyclicBarrier(int parties, Runnable barrierAction) {
if (parties <= ) throw new IllegalArgumentException();
this.parties = parties;
this.count = parties;
this.barrierCommand = barrierAction;
}
在进行实例化的时候可以现设置一个线程
CyclicBarrier cyclicBarrier=new CyclicBarrier(3,work3);当所有线程达到指定屏障点后启动work3线程
java CyclicBarrier的介绍和使用的更多相关文章
- java.util.concurrent介绍【转】
java.util.concurrent介绍 java.util.concurrent 包含许多线程安全.测试良好.高性能的并发构建块.不客气地说,创建 java.util.concurrent ...
- JAVA基本类库介绍
我们曾经讲过,Java已经为编程者编制了许多类,这些类已经经过测试,基本上不存在错误,这些类都是我们编程的基础.如果不利用这些已存在的类,我们的 编程工作将变得异常复杂,所以我们应尽可能多的掌握Jav ...
- Java垃圾回收介绍(译)
在Java中,对象内存空间的分配与回收是由JVM中的垃圾回收进程自动完成的.与C语言不同的是,在Java中开发者不需要专门为垃圾回收写代码.这是使Java流行的众多特征之一,也帮助了程序员写出了更好的 ...
- Java Web开发介绍
转自:http://www.cnblogs.com/pythontesting/p/4963021.html Java Web开发介绍 简介 Java很好地支持web开发,在桌面上Eclipse RC ...
- [译]Java 垃圾回收介绍
说明:这篇文章来翻译来自于Javapapers 的Java Garbage Collection Introduction 在Java中,对象内存空间的分配与回收是由JVM中的垃圾回收进程自动完成的. ...
- Java高新技术 Myeclipse 介绍
Java高新技术 Myeclipse 介绍 知识概述: (1)Myeclipse开发工具介绍 (2)Myeclipse常用开发步骤详解 ...
- [Java并发编程(三)] Java volatile 关键字介绍
[Java并发编程(三)] Java volatile 关键字介绍 摘要 Java volatile 关键字是用来标记 Java 变量,并表示变量 "存储于主内存中" .更准确的说 ...
- 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比
[原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...
- Java EE平台介绍(译)
Java EE平台介绍 2.1 企业应用总览 这一部分将对企业应用及其设计和开发进行简单介绍. 就像之前说的,Java EE 平台是为了帮助开发者开发大规模.多层次.可伸缩.服务可靠.网络安全的应用而 ...
随机推荐
- ORACLE grant权限
oracle的系统和对象权限 本文转自: http://hi.baidu.com/zhaojing_boy/blog/item/0ffe95091266d939e824885f.html alter ...
- HTTPS加密过程分析
HTTPS加密的作目的是解决信息传输过程中数据被篡改,窃取 HTTPS使用了一系列的加密算法:对称加密算法.非对称加密算法.单向加密算法 非对称加密算法 分为公钥部分和私钥部分,用公钥加密的密文只能由 ...
- Vue.js 生态之vue-router
vue-router是什么~~ vue-router是Vue的路由系统,定位资源的,我们可以不进行整页刷新去切换页面内容. vue-router的安装和基本配置 vue-router.js 可以下载 ...
- 简单的linux命令
1.cd命令: 这是一个最基本的命令,用于切换当前目录,可以是绝对路径,也可以是相对路径例: cd /root/doc #切换到目录/root/doc cd ./path 切换到当前目录下的pat ...
- Python+Selenium学习--键盘事件
场景 我们在实际的测试工作中,有时候需要使用tab键将焦点移动到下一个元素,用于验证元素的排序是否正确.webdriver的Keys()类提供键盘上所有的操作,甚至可以模拟一些组合键的操作,如Ctrl ...
- AD操作
加泪滴 批量添加覆铜过孔(先铺铜以后,再批量添加过孔) 开槽 在KEPP—OUT层 部分区域 不敷铜 开窗
- HDU 1698 Just a Hook(线段树区间更新查询)
描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...
- f5单台安装配置
1.对设备的管理口进行配置 管理口的默认设置 •IP 地址: 192.168.1.245/24 2.用户名和密码 1)初始密码: 图形界面用户名/密码:admin/admin:命令行用户名/密码:ro ...
- macOS 升级后重装命令行工具的问题
问题背景 最近升级个人macbook 从 10.13 到 10.14 在终端输入 git 不能用了,发现是重装操作系统后原来的 Command Line Tools 被自动卸载了, 采用 xcode- ...
- 关于js动画简单理解;
1.CSS样式提供了运动 过度的属性:transition 过度的属性值:attr ,time , liner , delay: 值分别是:属性(css),花费的时间,变化的速度(默认匀速), ...