CountDownLatch 简介和例子
来源:互联网 发布:微电影剪辑软件 编辑:程序博客网 时间:2024/04/30 16:12
public class CountDownLatchTest {
public static int numberOfPeople = 10;//等车的学生数
public static boolean isGone = false;//车开的标志
public static int carWaitTime = 3;//车等的时间
public static void main(String[] args) throws InterruptedException {
CountDownLatch waitStudentsGetOn = new CountDownLatch(numberOfPeople);
new Thread(new GetOn(waitStudentsGetOn)).start();
waitStudentGetOn(waitStudentsGetOn);//等所有的学生上车
driveHome();//开车走
}
private static void waitStudentGetOn(CountDownLatch waitStudentsGetOn) throws InterruptedException {
System.out.println("赶紧的,抓紧时间上车..");
waitStudentsGetOn.await(carWaitTime, TimeUnit.SECONDS);//等5秒,还没上车,就开走。。
}
private static void driveHome() throws InterruptedException {
System.out.println("开车,鞋儿破 帽儿破 身上的袈裟破 你笑我 他笑我 一把扇儿破");
isGone = true;
}
}
class GetOn implements Runnable{
private CountDownLatch waitStudentsGetOn;
GetOn(CountDownLatch waitStudentsGetOn){
this.waitStudentsGetOn = waitStudentsGetOn;
}
public void run() {
for (int i = 0; i < CountDownLatchTest.numberOfPeople; i++) {
try {
if(CountDownLatchTest.isGone){
System.out.println("妈的,还差:"+waitStudentsGetOn.getCount()+" 个没娃上车呢.怎么车走了");
break;
}
boolean goonSuccess = new Student(i+1).getOn();//顺序上车
if(goonSuccess)waitStudentsGetOn.countDown();
} catch (InterruptedException e) {}
if(waitStudentsGetOn.getCount()!=0l){
System.out.println("还差:"+(waitStudentsGetOn.getCount())+" 个没上车");
}else{
System.out.println("都上车了");
}
}
}
class Student{
private int myNum;//学生编号
public Student(int num){
this.myNum = num;
}
//上车
public boolean getOn() throws InterruptedException{
Thread.currentThread().sleep(new Random().nextInt(2)*1000);//上车使用的时间,随机
if(CountDownLatchTest.isGone){
return false;//不能上了,上车失败
}
System.out.print("编号为:"+myNum+"的同学上车了..");
return true;
}
}
}
public static int numberOfPeople = 10;//等车的学生数
public static boolean isGone = false;//车开的标志
public static int carWaitTime = 3;//车等的时间
public static void main(String[] args) throws InterruptedException {
CountDownLatch waitStudentsGetOn = new CountDownLatch(numberOfPeople);
new Thread(new GetOn(waitStudentsGetOn)).start();
waitStudentGetOn(waitStudentsGetOn);//等所有的学生上车
driveHome();//开车走
}
private static void waitStudentGetOn(CountDownLatch waitStudentsGetOn) throws InterruptedException {
System.out.println("赶紧的,抓紧时间上车..");
waitStudentsGetOn.await(carWaitTime, TimeUnit.SECONDS);//等5秒,还没上车,就开走。。
}
private static void driveHome() throws InterruptedException {
System.out.println("开车,鞋儿破 帽儿破 身上的袈裟破 你笑我 他笑我 一把扇儿破");
isGone = true;
}
}
class GetOn implements Runnable{
private CountDownLatch waitStudentsGetOn;
GetOn(CountDownLatch waitStudentsGetOn){
this.waitStudentsGetOn = waitStudentsGetOn;
}
public void run() {
for (int i = 0; i < CountDownLatchTest.numberOfPeople; i++) {
try {
if(CountDownLatchTest.isGone){
System.out.println("妈的,还差:"+waitStudentsGetOn.getCount()+" 个没娃上车呢.怎么车走了");
break;
}
boolean goonSuccess = new Student(i+1).getOn();//顺序上车
if(goonSuccess)waitStudentsGetOn.countDown();
} catch (InterruptedException e) {}
if(waitStudentsGetOn.getCount()!=0l){
System.out.println("还差:"+(waitStudentsGetOn.getCount())+" 个没上车");
}else{
System.out.println("都上车了");
}
}
}
class Student{
private int myNum;//学生编号
public Student(int num){
this.myNum = num;
}
//上车
public boolean getOn() throws InterruptedException{
Thread.currentThread().sleep(new Random().nextInt(2)*1000);//上车使用的时间,随机
if(CountDownLatchTest.isGone){
return false;//不能上了,上车失败
}
System.out.print("编号为:"+myNum+"的同学上车了..");
return true;
}
}
}
- CountDownLatch 简介和例子
- CountDownLatch 简介和例子
- CountDownLatch简介
- 记录一下CountDownLatch 和 CyclicBarrier 的使用例子
- AWK 简介和例子
- javaBean简介和例子
- WebSocket简介和例子
- CountDownLatch 使用例子
- CountDownLatch的使用例子
- 一个例子学会CountDownLatch
- CountDownLatch 之简介.
- 1.CountDownLatch简介
- java RMI简介和例子
- Mapstruct简介和简单例子
- expect简介和使用例子
- 同步器CountDownLatch的应用例子
- java 并发包 CountDownLatch 例子
- Java-CountDownLatch的小例子
- A story about love
- 报错:Collection <__NSCFDictionary: 0xc425570> was mutated while being enumerated.
- Delphi调用Tuxedo
- 移植内核支持usb摄像头(钰创1e4e/0100)
- Linux环境进程间通信-信号量
- CountDownLatch 简介和例子
- Handler消息传递机制
- 内存的堆分配和栈分配 & 字符数组,字符指针,Sizeof总结
- C#读写app.config中的数据
- Java–排序算法-总结
- 模板函数template functions和模板类的基本区别template class
- SQLite数据库总结
- 两种HTTP连接方式POST&GET的比较
- C#读取对象实例的值和对对象的属性自动赋值方法