在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,今天我们就来学习一下这countdownlatch类的用法。
CountDownLatch的用法非常简单,下面的例子也是我在网上看到的,十分贴切,这里就贴出来
public class Test { public static void main(String[] args) { CountDownLatch begin = new CountDownLatch(1); CountDownLatch end = new CountDownLatch(2); for(int i=0; i<2; i++){ Thread thread = new Thread(new Player(begin,end)); thread.start(); } try{ System.out.println("the race begin"); begin.countDown(); end.await(); System.out.println("the race end"); }catch(Exception e){ e.printStackTrace(); } } } /** * 选手 */ class Player implements Runnable{ private CountDownLatch begin; private CountDownLatch end; Player(CountDownLatch begin,CountDownLatch end){ this.begin = begin; this.end = end; } public void run() { try { begin.await(); System.out.println(Thread.currentThread().getName() + " arrived !");; end.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } } }
下面是运行结果
可以看到 通过CountDownLatch 的使用 我们控制了线程的执行顺序。
在上面代码中,我们使用到await()方法 和 countDown() 方法 。我们验证一下它们各自的作用。
首先 验证await() 方法。将main方法中的 end.await() 注释掉,下面是注释掉后的运行结果
可以看到主线程没有等待代表选手的线程结束,直接宣布比赛结束了!刚开始就结束的比赛- -
这里可以看出,await() 方法具有阻塞作用
其次 我们来验证countDown方法,将代表选手线程中的 end.countDown() 进行注释,下面是运行结果
程序一直在运行,所有选手都已经到了终点,但是裁判就是不宣传比赛结束,他在等什么呢?
我们猜测countDown() 方法具有唤醒阻塞线程的作用。
那我们也许会问,既然有唤醒阻塞线程的作用,那么我们只调用一次countDown() 方法不就是可以唤醒被阻塞的主线程了吗?
我们试一下,取消上面coutDown()的注释,再次创建一个选手,代码如下
class Player2 implements Runnable{ private CountDownLatch begin; private CountDownLatch end; Player2(CountDownLatch begin,CountDownLatch end){ this.begin = begin; this.end = end; } public void run() { try { begin.await(); System.out.println(Thread.currentThread().getName() + " arrived !"); // end.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } } }
main 方法也修改如下,创建了两个不同的选手
public static void main(String[] args) { CountDownLatch begin = new CountDownLatch(1); CountDownLatch end = new CountDownLatch(2); Thread thread = new Thread(new Player(begin, end)); thread.start(); Thread thread2 = new Thread(new Player2(begin, end)); thread2.start(); try { System.out.println("the race begin"); begin.countDown(); end.await(); System.out.println("the race end"); } catch (Exception e) { e.printStackTrace(); } }
运行一下,下面是结果
主程序一直阻塞,没有被唤醒,裁判上厕所上得有点久啊!
这样看来countDown() 并不是直接唤醒线程,有点像一个计数器,倒计时的那种。
查看API文档,果然,我们在构造函数中添加了参数2,就需要调用 2 次 countDown() 才能将 end.await() 阻塞的线程唤醒。
CountDownLatch end = new CountDownLatch(2);
总结一下,
1、CountDownLatch end = new CountDownLatch(N); //构造对象时候 需要传入参数N
2、end.await() 能够阻塞线程 直到调用N次end.countDown() 方法才释放线程
3、end.countDown() 可以在多个线程中调用 计算调用次数是所有线程调用次数的总和
评论时间:2022-12-09 15:25:01
我们提供了一些简单而实用的方法来帮助大家快速创建站点,这里就不详阿里云云虚拟主机管理平台 细介绍了云主机备份软件 详细介绍了
评论时间:2022-09-14 21:25:01
12.接着进入“工网页服务器搭建 具→任务管理器”,选中“新建项目—>海外虚拟主机租用平台 选中“新建项目—>更改名称”复选框后即可进行项目的修改了
2023-12-25 22:44:35 admin
活动发布区版规2023-05-27 22:03:52 御风而行
容器、无服务器、虚拟机:安全性差...2023-05-27 20:43:39 1356
服务器宕机的造成原因和解决方法介...2023-05-27 20:43:25 qingym
别糊涂了!服务器与工作站的五大区...2023-05-27 19:08:41 小绵羊0123
科技巨头布局云端 服务器 网络硬...2023-05-27 18:17:46 姐姐的跟屁虫
钉钉因系统访问流量激增,宕机1小...2022-07-23 00:34:02 freeatom
常见ftp命令 FTP命令使用详...2022-07-21 02:17:02 ares
双硬盘组建Raid0磁盘阵列图文...2022-07-20 06:17:02 mankeung123
用友软件客户端连接不上服务器的解...2022-07-23 00:51:02 antonfxb
WebService实例2022-07-13 05:38:02 苯小孩
开发、运维不可不看的Linux调...2022-07-20 18:51:02 nightstone
如何使用Charles抓取Web...2021-12-25 05:38:01 青青子w
石家庄服务器托管并不局限于石家庄...2022-02-05 05:38:02 中原一点红
基于域名的虚拟主机配置的两种方法2022-07-19 06:17:02 koushuiduo
使用IIS服务配置Web服务器(...2022-07-19 06:34:02 hillajun12
Web服务器配置(上)2022-07-19 09:51:02 99225
Squid代理服务器原理2022-07-19 14:17:02 edisonpeng
红旗linux视频教程-linu...2022-07-19 20:17:02 dengyu0429
linux vi使用及详细介绍2022-07-20 01:00:05 激动的舌头
Linux视频教程:用户管理命令...2022-07-21 20:51:02 jessica-an
创建本地FTP站点2022-02-07 05:38:03 jasonkidd
WEB服务器配置详解2022-07-20 04:51:02 wolfssss
ACL权限-默认与递归(4)2022-07-22 15:00:05 淡水珊瑚
Linux下 Samba Ser...