Java多线程之~~~使用CountDownLatch来同步多个线程实现一个任务
来源:互联网 发布:淘宝7寸平板电脑 编辑:程序博客网 时间:2024/05/21 11:17
在多线程开发中,经常会遇到这样的问题,比如,一个线程需要其他的一些辅助线程完成指定的一些任务后才能开
启。 类似于一个主线程正在运行,他需要其他分支线程完成一些任务后才能激活他来启动剩下的任务,这里就可以使用
Java自带的CountDownLatch这个类来帮我们实现这样的效果。 这个类初始化的时候会指定一个数字,这就是需要等
待的资源的数量,每一个资源到位的时候,就调用他的countDown函数,这样就会将资源减一,知道这个资源数字变成
0的时候 ,就会叫醒主线程,来完成剩下的功能,下面我们就使用一个例子来说明这样的情况。
package com.bird.concursey.charpet4;import java.util.concurrent.CountDownLatch;public class Videoconference implements Runnable {private CountDownLatch controller;/** * Implement the constructor of the class that initializes the CountDownLatchattribute. The Videoconference class will wait for the arrival of the number ofparticipants received as a parameter. * @param number */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("VideoConference: Waiting for %d participants.\n",controller.getCount());}@Overridepublic void run() {System.out.printf("VideoConference: Initialization: %d participants.\n",controller.getCount());try {controller.await();System.out.printf("VideoConference: All the participants have come\n");System.out.printf("VideoConference: Let's start...\n");} catch (InterruptedException e) {e.printStackTrace();}}}
package com.bird.concursey.charpet4;import java.util.concurrent.TimeUnit;public class Participant implements Runnable {private Videoconference videoConference;private String name;public Participant(Videoconference videoConference, String name) {this.videoConference = videoConference;this.name = name;}@Overridepublic void run() {long duration=(long)(Math.random()*10);try {TimeUnit.SECONDS.sleep(duration);} catch (InterruptedException e) {e.printStackTrace();}videoConference.arrive(name);}public static void main(String[] args) {//Create a Videoconference object named conference that waits for 10 participants.Videoconference videoCOnference = new Videoconference(10);Thread threadConference = new Thread(videoCOnference);threadConference.start();for(int i = 0; i < 10; i++) {Participant participant = new Participant(videoCOnference, "participant" + i);Thread t = new Thread(participant);t.start();}}}
3 0
- Java多线程之~~~使用CountDownLatch来同步多个线程实现一个任务
- JAVA多线程—CountDownLatch-一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
- java多线程(13)--线程同步工具之CountDownLatch
- Java并发和多线程4:使用通用同步工具CountDownLatch实现线程等待
- java线程同步之CountDownLatch
- java多线程中线程同步工具-CountDownLatch
- java多线程同步工具之CountDownlatch
- Java中使用CountDownLatch进行多线程同步
- 线程同步之CountDownLatch
- 采用Thread.join()或CountDownLatch来实现线程间同步
- 多线程编程入门(17):线程同步工具之CountDownLatch
- Java多线程--同步计数器CountDownLatch
- 【CountDownLatch】实现多个线程同时启动
- Android之CountDownLatch线程同步
- 线程同步工具之CountDownLatch
- 线程同步工具之CountDownLatch
- 线程同步工具之CountDownLatch
- 线程同步工具之CountDownLatch
- ios手势识别(双击、捏、旋转、拖动、划动、长按, 上下左右滑动)
- 开源企业IM-免费企业即时通讯-ENTBOOST v1.2版本发布
- 腾讯qq企业邮箱免费邀请码46D08DB9
- pfRing透明工作模式
- Android,自定义一个点击变暗效果的ImageView
- Java多线程之~~~使用CountDownLatch来同步多个线程实现一个任务
- 用 Swift 开发一个TODO应用
- jQuery中wrap、wrapAll和wrapInner用法以及区别
- POJ 2406 Power Strings 解题报告(哈希)
- 联想2015校园招聘笔试题
- web工作流管理系统开发之四 自定义表单
- 初探 iOS8 中的 Size Class
- Java之内存分析和String对象
- Swift的函数式API