countdownlatch

CountDownLatch

帅比萌擦擦* 提交于 2019-11-27 03:38:22
CountDownLatch是 java.util.concurrent 包下的线程同步类,并发环境下线程对计数值减1操作,当计数值为0时,被wait阻塞的线程将被唤醒,达到线程同步. 该类涉及到的主要方法: // 当前线程在计数值减到0之前一直等待,除非当前线程被中断 void await() // 当前线程在计数值减到0之前一直等待,除非当前线程被中断或者超过了指定的等待时间 boolean await(long timeout, TimeUnit unit) // 减少计数值,当计数值减到0时,释放所有的等待线程 void countDown() // 返回当前计数值 long getCount() 示例: CountDownLatch countDownLatch = new CountDownLatch(3); ExecutorService executorService = Executors.newFixedThreadPool(3); for (int i = 0; i < 3; i++) { int finalI = i; executorService.execute(new Runnable() { @Override public void run() { System.out.println("任务执行结束啦,i:" + finalI + "," +

CountDownLatch原理分析

℡╲_俬逩灬. 提交于 2019-11-27 02:37:50
CountDownLatch原理分析 CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程执行完后再执行。例如,应用程序的主线程希望在负责启动框架服务的线程已经启动所有框架服务之后执行。 CountDownLatch使用示例: 首先我们写一个示例,看看怎么使用用CountDownLatch工具类 CountDownLatchTest.java package com.study.thread.juc_thread.base; import java.util.concurrent.CountDownLatch; /** * <p>Description: CountDownLatch 在所有任务结束之前,一个或者多线线程可以一直等待(通过计数器来判断) </p> * @author duanfeixia * @date 2019年8月12日 */ public class CountDownLatchTest { static CountDownLatch countDown = new CountDownLatch(10);//10个线程任务 /** * <p>Description: 主线程执行任务</p> * @author duanfeixia * @date 2019年8月12日 */ static class BossThread

AQS

痞子三分冷 提交于 2019-11-27 02:32:42
文章目录 介绍 原理 AQS 原理概览 AQS 对资源的共享方式 AQS底层使用了模板方法模式 AQS 组件总结 介绍 AQS的全称为(AbstractQueuedSynchronizer),这个类在 java.util.concurrent.locks 包下面。 AQS是一个 用来构建锁和同步器的框架 ,使用AQS能简单且高效地构造出应用广泛的大量的同步器,比如我们提到的ReentrantLock,Semaphore,其他的诸如ReentrantReadWriteLock,SynchronousQueue,FutureTask等等皆是基于AQS的。当然,我们自己也能利用AQS非常轻松容易地构造出符合我们自己需求的同步器。 原理 AQS 原理概览 AQS核心思想是, 如果被请求的共享资源空闲,则将当前请求资源的线程设置为有效的工作线程,并且将共享资源设置为锁定状态 。 如果被请求的共享资源被占用,那么就需要一套线程阻塞等待以及被唤醒时锁分配的机制 ,这个机制AQS是用 CLH队列锁 实现的,即 将暂时获取不到锁的线程加入到队列 中。 CLH (Craig,Landin,and Hagersten)队列是一个 虚拟的双向队列 (虚拟的双向队列即不存在队列实例,仅存在结点之间的关联关系)。AQS是将每条请求共享资源的线程封装成一个CLH锁队列的一个结点(Node)来实现锁的分配。

Java concurrency: Countdown latch vs Cyclic barrier

拜拜、爱过 提交于 2019-11-27 02:29:28
I was reading through the java.util.concurrent API , and found that CountDownLatch : A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. CyclicBarrier : A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. To me both seems equal, but I am sure there is much more to it. For example, in CoundownLatch, the countdown value could not be reset, that can happen in the case of CyclicBarrier . Is there any other difference between the two? What are the use cases

CountDownLatch 源码解析—— countDown()

蹲街弑〆低调 提交于 2019-11-27 02:08:28
上一篇文章 从源码层面说了一下CountDownLatch 中 await() 的原理。这篇文章说一下countDown() 。 public void countDown() { //CountDownLatch sync.releaseShared(1); } ↓ public final boolean releaseShared(int arg) { //AQS if (tryReleaseShared(arg)) { doReleaseShared(); return true; } return false; } ↓ protected boolean tryReleaseShared(int releases) { //CountDownLatch.Sync // 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; } } 通过构造器 CountDownLatch end = new CountDownLatch(2); state 被设置为2,所以c == 2,nextc

java Fork/Join pool, ExecutorService and CountDownLatch

孤街醉人 提交于 2019-11-27 01:43:20
问题 We have three different multi threading techniques in java - Fork/Join pool, Executor Service & CountDownLatch Fork/Join pool (http://www.javacodegeeks.com/2011/02/java-forkjoin-parallel-programming.html) The Fork/Join framework is designed to make divide-and-conquer algorithms easy to parallelize. That type of algorithms is perfect for problems that can be divided into two or more sub-problems of the same type. They use recursion to break down the problem to simple tasks until these become

1.3.4 并发工具类CountDownLatch/Semaphore/CyclicBarrier/FutureTask

匆匆过客 提交于 2019-11-27 00:50:32
CountDownLatch的2个用途: 1. 所有线程都到达相同的起跑线后,再一起开始跑(并非同时开始,而是队列中一个唤醒另一个)【此情况需到达起跑线后再调用await()等待其他线程】 2. 所有线程都到达终点(执行完)后,再一起庆祝 (并非同时开始,而是队列中一个唤醒另一个)【此情况需到达起终点后再调用await()等待其他线程】 package com.study.concurrent_utils; import java.util.concurrent.CountDownLatch; public class Test_CountDownLatch { /* * 没隔1s开启一个线程,共开启6个线程 * 若希望6个线程 同时 执行某一操作 * 可以用CountDownLatch实现 */ public static void test01() throws InterruptedException { CountDownLatch ctl = new CountDownLatch(6); for (int i = 0; i < 6; i++) { new Thread() { @Override public void run() { ctl.countDown(); try { ctl.await(); // 6个线程都启动执行到此处时,打印如下 System.out

How to wait for a thread that spawns it's own thread?

只愿长相守 提交于 2019-11-26 23:35:16
问题 I'm trying to test a method that does it's work in a separate thread, simplified it's like this: public void methodToTest() { Thread thread = new Thread() { @Override public void run() { Clazz.i = 2; } }; thread.start(); } In my unit test I want to test that Clazz.i == 2, but I can't do this because I think that the assert is run before the thread changes the value. I thought of using another thread to test it and then use join to wait but it still doesn't work. SSCCE: @Test public void sscce

CountDownLatch vs. Semaphore

馋奶兔 提交于 2019-11-26 23:23:01
Is there any advantage of using java.util.concurrent.CountdownLatch instead of java.util.concurrent.Semaphore ? As far as I can tell the following fragments are almost equivalent: 1. Semaphore final Semaphore sem = new Semaphore(0); for (int i = 0; i < num_threads; ++ i) { Thread t = new Thread() { public void run() { try { doStuff(); } finally { sem.release(); } } }; t.start(); } sem.acquire(num_threads); 2: CountDownLatch final CountDownLatch latch = new CountDownLatch(num_threads); for (int i = 0; i < num_threads; ++ i) { Thread t = new Thread() { public void run() { try { doStuff(); }

Java的一些并发包

≡放荡痞女 提交于 2019-11-26 19:07:34
同步容器类 Vector和ArayList : ArrayList是最常用的List实现类,内部是通过数组实现的,它允许对元素进行快速随机访问。数组的缺点是每个元素之间不能有间隔,当数组大小不满足时需要增加存储能力,就要讲已经有数组的数据复制到新的存储空间中。当从ArrayList的中间位置插入或者删除元素时,需要对数组进行复制、移动、代价比较高。因此,它适合随机查找和遍历,不适合插入和删除。 Vector与ArrayList一样, 也是通过数组实现的,不同的是它支持线程的同步 ,即某一时刻只有一个线程能够写Vector,避免多线程同时写而引起的不一致性,但实现同步需要很高的花费,因此,访问它比访问ArrayList慢。Vector与ArrayList的扩容并不一样,Vector默认扩容是增长一倍的容量,Arraylist是增长50%的容量 注意: Vector线程安全、ArrayList Vector.add源码: ​ ArrayList.add源码: ​ 由此,看出,Vectory方法使用synchronized同步函数方法写的,线程同步。 HashMap和HashTable: 1.HashMap不是线程安全的 ,HastMap是一个接口 是map接口的子接口,是将键映射到值的对象,其中键和值都是对象,并且不能包含重复键,但可以包含重复值。HashMap允许null