JUC CountDownLach原理是什么
发表于:2025-11-20 作者:千家信息网编辑
千家信息网最后更新 2025年11月20日,本篇内容主要讲解"JUC CountDownLach原理是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"JUC CountDownLach原理是什么"
千家信息网最后更新 2025年11月20日JUC CountDownLach原理是什么
本篇内容主要讲解"JUC CountDownLach原理是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"JUC CountDownLach原理是什么"吧!
CountDownLach闭锁
背景
CountDownLatch是在Java1.5被引入,跟它一起被引入的工具类还有CyclicBarrier、Semaphore、ConcurrenthashMap和BlockingQueue。
在java.util.cucurrent包下。
概念
CountDownLatch这个类使一个线程等待其它线程各自执行完毕后再执行。
是通过一个计数器来实现的,计数器的初始值是线程的数量。每当一个线程执行完毕后,计数器的值就-1,当计数器的值为0时,表示所有线程都执行完毕,然后在闭锁上等待的线程就可以恢复工作来。
源码
countDownLatch类中只提供了一个构造器
public CountDownLatch(int count) { if (count < 0) throw new IllegalArgumentException("count < 0"); this.sync = new Sync(count); }类中有三个方法是最重要的
// 调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行 public void await() throws InterruptedException { sync.acquireSharedInterruptibly(1); }//和await()方法类似,只不过等待一定的时间后count值还没变为0的化就会继续执行 public boolean await(long timeout, TimeUnit unit) throws InterruptedException { return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout)); }//将count值减1 public void countDown() { sync.releaseShared(1); }示例
普通示例:
public class CountDownLatchTest { public static void main(String[] args) { final CountDownLatch latch = new CountDownLatch(2); System.out.println("主线程开始执行…… ……"); //第一个子线程执行 ExecutorService es1 = Executors.newSingleThreadExecutor(); es1.execute(new Runnable() { @Override public void run() { try { Thread.sleep(3000); System.out.println("子线程:"+Thread.currentThread().getName()+"执行"); } catch (InterruptedException e) { e.printStackTrace(); } latch.countDown(); } }); es1.shutdown(); //第二个子线程执行 ExecutorService es2 = Executors.newSingleThreadExecutor(); es2.execute(new Runnable() { @Override public void run() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("子线程:"+Thread.currentThread().getName()+"执行"); latch.countDown(); } }); es2.shutdown(); System.out.println("等待两个线程执行完毕…… ……"); try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("两个子线程都执行完毕,继续执行主线程"); } }结果集:
主线程开始执行…… …… 等待两个线程执行完毕…… ……子线程:pool-1-thread-1执行子线程:pool-2-thread-1执行两个子线程都执行完毕,继续执行主线程
模拟并发示例:
public class Parallellimit { public static void main(String[] args) { ExecutorService pool = Executors.newCachedThreadPool(); CountDownLatch cdl = new CountDownLatch(100); for (int i = 0; i < 100; i++) { CountRunnable runnable = new CountRunnable(cdl); pool.execute(runnable); } }} class CountRunnable implements Runnable { private CountDownLatch countDownLatch; public CountRunnable(CountDownLatch countDownLatch) { this.countDownLatch = countDownLatch; } @Override public void run() { try { synchronized (countDownLatch) { /*** 每次减少一个容量*/ countDownLatch.countDown(); System.out.println("thread counts = " + (countDownLatch.getCount())); } countDownLatch.await(); System.out.println("concurrency counts = " + (100 - countDownLatch.getCount())); } catch (InterruptedException e) { e.printStackTrace(); } }}源码分析
public class CountDownLatch { //继承AQS来实现他的模板方法(tryAcquireShared,tryReleaseShared) private static final class Sync extends AbstractQueuedSynchronizer { //计数个数Count Sync(int count) { setState(count); } int getCount() { return getState(); } //AQS方法getState(),返回同步状态,这里指计数器值 protected int tryAcquireShared(int acquires) { return (getState() == 0) ? 1 : -1; } //循环+cas重试 直到计数器为0 跳出,则release(实现aqs共享模式释放方法) protected boolean tryReleaseShared(int releases) { // Decrement count; signal when transition to zero for (;;) { int c = getState(); if (c == 0) return false; int nextc = c-1; if (compareAndSetState(c, nextc)) return nextc == 0; } } } private final Sync sync; //实例化 public CountDownLatch(int count) { if (count < 0) throw new IllegalArgumentException("count < 0"); this.sync = new Sync(count); } public void await() throws InterruptedException { sync.acquireSharedInterruptibly(1); } //带有一个超时时间的awit public boolean await(long timeout, TimeUnit unit) throws InterruptedException { return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout)); } public void countDown() { sync.releaseShared(1); } public long getCount() { return sync.getCount(); }}到此,相信大家对"JUC CountDownLach原理是什么"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
线程
方法
计数器
两个
原理
示例
个子
内容
时间
源码
学习
实用
普通
更深
重要
三个
个数
兴趣
只不过
实例
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
计算机网络技术 招聘
数据库逻辑独立性是由
贵州毕节互联网服务器云主机
网络安全认证考试考什么
我的世界服务器ess
六盘水软件开发
家里用不用安装服务器
魔域数据库汉化版
英国网络安全硕士排名
智能网络技术开发行业标准
南京先进网络技术收购价格
软件开发甲方不验收
服务器无法连接什么意思
数据库三范式的含义
fwd软件开发
网络安全人才卓越计划
贵州正规软件开发多少钱
软件开发过程中开发计划表
公司内部dns服务器地址
网络安全学习线路
五一孩子网络安全
服务器为什么要4个网口
南京鹏的软件开发怎么样
cc服务器有哪些类型
网络技术应用实际应用
服务器无法连接什么意思
庆余年服务器互通吗
阿里云云服务器cpu负载过高
怎么访问网络ip数据库
上海工商金融网络技术服务优势