java多线程 等待多个并发事件的完成
2016-02-11 19:24
603 查看
java API中提供了CountDownLatch类,它允许线程一直等待,知道等待操作结束。下面以一个视频会议等待人数为例。
1.视频会议类,实现Runnable接口。
2.与会者类
3.主方法
当创建CountDownLatch对象时,通过构造器来初始化内部计数器(本例中为10),当countdown()方法被调用时,计数器将减1,当计数器为0时,countdownlatch将唤醒所有等待的线程。
注意点:
CountDownLatch对象计数器一旦被初始化之后,就不可以修改。
他并不是用来保护临界资源或者同步代码块,只是用来同步多个任务的多个线程。
CountDownLatch对象只能被使用一次,一旦计数器为0时,如果再需要使用,则需要重新New一个CountDownLatch对象。
1.视频会议类,实现Runnable接口。
import java.util.concurrent.CountDownLatch; public class Videoconference implements Runnable { private final CountDownLatch controller; public Videoconference(int number){ controller = new CountDownLatch(number); } //每一个视频会议者到达是都执行此方法 public void arrive(String name){ System.out.printf("%s has arrived.",name); controller.countDown(); //还没有到达的人数 System.out.printf("Videoconferencr has waitinf for %d participants\n",controller.getCount()); } public void run() { System.out.printf("Videoconference initialization:%d participants.\n",controller.getCount()); try { //等待所有人到达,如果没有这句,则会在参与人没到达之前开始会议 controller.await(); System.out.printf("Videoconference:ALL participants has come\n"); System.out.print("Videoconference:Let is start...\n"); } catch (InterruptedException e) { e.printStackTrace(); } } }
2.与会者类
import java.util.concurrent.TimeUnit; public class Participant implements Runnable{ private Videoconference conference; public String name; public Participant(Videoconference conference,String name){ this.conference = conference; this.name = name; } @Override public void run() { long duration = (long)(Math.random()*10); try { TimeUnit.SECONDS.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } conference.arrive(name); } }
3.主方法
package Thread03semaphone; public class Main2 { public static void main(String[] args){ Videoconference videoConference = new Videoconference(10); Thread threadconference = new Thread(videoConference); threadconference.start(); for(int i =0;i<10;i++){ Participant p = new Participant(videoConference,"Participant"+i); Thread t = new Thread(p); t.start(); } } }
当创建CountDownLatch对象时,通过构造器来初始化内部计数器(本例中为10),当countdown()方法被调用时,计数器将减1,当计数器为0时,countdownlatch将唤醒所有等待的线程。
注意点:
CountDownLatch对象计数器一旦被初始化之后,就不可以修改。
他并不是用来保护临界资源或者同步代码块,只是用来同步多个任务的多个线程。
CountDownLatch对象只能被使用一次,一旦计数器为0时,如果再需要使用,则需要重新New一个CountDownLatch对象。
相关文章推荐
- 蓝桥杯 大小写转换
- eclipse自定义设置tab页切换快捷键
- spring前端显示
- 【慕课笔记】U4 多态 第3节 JAVA中的抽象类
- java基础总结 --- enum枚举的常量相关方法、自动售货机(状态模式)例子
- Java学习笔记5-泛型
- 在eclipese中编写servlet时出现“The import javax.servlet cannot be resolved”
- javabean技术
- Java8新特行学习
- Java—进程间通讯的有几种方法
- The Java™ Tutorials — Concurrency :Synchronization 同步
- Java theory and practice: More flexible, scalable locking in JDK 5.0
- Java线程池学习笔记
- java数据类型
- struts2学习笔记--OGNL表达式1
- Java核心技术(第8版)学习笔记_基本的程序设计结构
- Java的多线程之同步篇一:锁对象、条件对象
- java 反射机制
- java volatile详解(转)
- JavaBean 和内省