CountDownLacth的使用
来源:互联网 发布:mac锁屏快捷键 编辑:程序博客网 时间:2024/06/14 10:35
package myAllTest.TCountDownLatch;
import java.util.concurrent.CountDownLatch;
public class TCountDownLatch {
/**
*
* 这个类的作用是为了测试一下CountDownLacth这个类
* 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
* @param args
*/
public static void main(String[] args) {
TCountDownLatch td=new TCountDownLatch();
CountDownLatch latch=new CountDownLatch(2);
Worker work1=td.new Worker("bb",latch);
Worker work2=td.new Worker("aa",latch);
work1.start();
work2.start();
// try {
// latch.await();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
System.out.println("结束");
}
*
* 这个类的作用是为了测试一下CountDownLacth这个类
* 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
* @param args
*/
public static void main(String[] args) {
TCountDownLatch td=new TCountDownLatch();
CountDownLatch latch=new CountDownLatch(2);
Worker work1=td.new Worker("bb",latch);
Worker work2=td.new Worker("aa",latch);
work1.start();
work2.start();
// try {
// latch.await();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
System.out.println("结束");
}
class Worker extends Thread
{
private String name;
private CountDownLatch latch;
public Worker(String name,CountDownLatch latch) {
// TODO Auto-generated constructor stub
this.name=name;
this.latch=latch;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("name: "+name+" in");
this.delay();
System.out.println("name: "+name+" out");
//完成工作计数器减一
latch.countDown();
}
public void delay()
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
{
private String name;
private CountDownLatch latch;
public Worker(String name,CountDownLatch latch) {
// TODO Auto-generated constructor stub
this.name=name;
this.latch=latch;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("name: "+name+" in");
this.delay();
System.out.println("name: "+name+" out");
//完成工作计数器减一
latch.countDown();
}
public void delay()
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
这个时将注释去掉之后的结果
阅读全文
1 0
- CountDownLacth的使用
- CyclicBarrier和CountDownLacth
- 【多线程_并发工具】 闭锁-CountDownLacth
- GUID使用的使用
- ${}的使用
- 的使用
- @[],@()的使用
- ->的使用
- +、-、*、/、%的使用
- &&的使用
- %:的使用
- Makefile中使用$$的使用
- Makefile中使用$$的使用
- Red5的使用--使用日志
- prefernce的使用具体使用
- 【工具使用】git 的使用
- 关于使用AsyncTaskLoader的使用
- 使用CABasicAnimation的基本使用
- Bootstrap Multiselect插件使用步骤以及常见参数配置介绍
- toast.js
- WinScp和SecureCRT8.0的安装和使用
- ThreadLocal
- 无缝滑动
- CountDownLacth的使用
- Maven Web+SSH 项目搭建(Eclipse)
- ORA-12154: TNS: 无法解析指定的连接标识符(原创)
- Python 守护进程
- 一个数学问题
- 使用docker selenium 构建web自动化分布式测试环境时,遇到的一些问题
- document.all用法
- 【CRM项目】jQuery修改input的type属性失败
- 04.05 Linux系统用户与用户组