countdownlatch

多线程

*爱你&永不变心* 提交于 2019-11-28 03:56:13
import com.google.common.collect.Lists; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; /** * @author shiqil.liu * @date 2019-08-21 14:26 */ public class Lotxc { static Integer w = 0; static AtomicInteger ww = new AtomicInteger(0); static CountDownLatch countDownLatch = new CountDownLatch(200000); public static void main

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

試著忘記壹切 提交于 2019-11-28 01:51:45
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() throws InterruptedException { Thread thread = new Thread() { @Override public void run() {

并发编程--并发编程框架概述

拟墨画扇 提交于 2019-11-28 01:44:06
JDK给我们提供了一个并发编程的包java.util.current,并发编程包中是锁功能更加强大,并且他允许更灵活的使用锁。 JUC包中的锁,包括:Lock接口,ReadWriteLock接口,LockSupport阻塞原语,Condition条件,AbstractOwnableSynchronizer/AbstractQueuedSynchronizer/AbstractQueuedLongSynchronizer三个抽象类,ReentrantLock独占锁,ReentrantReadWriteLock读写锁。由于CountDownLatch,CyclicBarrier和Semaphore也是通过AQS来实现的;因此,我也将它们归纳到锁的框架中进行介绍。 先看看锁的框架图,如下所示。 1、Lock接口 JUC包中的 Lock 接口支持那些语义不同(重入、公平等)的锁规则。所谓语义不同,是指锁可是有"公平机制的锁"、"非公平机制的锁"、"可重入的锁"等等。"公平机制"是指"不同线程获取锁的机制是公平的",而"非公平机制"则是指"不同线程获取锁的机制是非公平的","可重入的锁"是指同一个锁能够被一个线程多次获取。 2、ReadWriteLock读写锁接口 ReadWriteLock 接口以和Lock类似的方式定义了一些读取者可以共享而写入者独占的锁。JUC包只有一个类实现了该接口

Java并发编程:CountDownLatch、CyclicBarrier和Semaphore

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 19:29:20
一.CountDownLatch用法   CountDownLatch类位于java.util.concurrent包下,利用它可以实现类似计数器的功能。比如有一个任务A,它要等待其他4个任务执行完毕之后才能执行,此时就可以利用CountDownLatch来实现这种功能了。 CountDownLatch类只提供了一个构造器: public CountDownLatch(int count) { }; //参数count为计数值 然后下面这3个方法是CountDownLatch类中最重要的方法: public void await() throws InterruptedException { }; //调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行 public boolean await(long timeout, TimeUnit unit) throws InterruptedException { }; //和await()类似,只不过等待一定的时间后count值还没变为0的话就会继续执行 public void countDown() { }; //将count值减1 下面看一个例子大家就清楚CountDownLatch的用法了: public class Test { public static void main(String[]

JUC基本使用——并发集合和同步工具类

╄→尐↘猪︶ㄣ 提交于 2019-11-27 16:21:33
并发集合 普通的集合中有List(ArrayList|LinkedList)、Set(HashSet|TreeSet)和Map(HashMap|TreeMap)集合,这些集合只适合在单线程情况下使用。 在Collecionts工具类中有synchronized开头方法可以把单线程集合转成支持并发的集合,但是效率不高,很少使用。 为了更好的实现集合的高并发访问处理,JUC创建了一组新的集合工具类,其使用和一般的集合一样,使用方法参照 “Java之集合” 的分类。 1. List和Set集合 CopyOnWriteArrayList相当于线程安全的ArrayList,实现了List接口,支持高并发 CopyOnWriteArraySet相当于线程安全的HashSet,它继承了AbstractSet类,内部是通过CopyOnWriteArrayList实现的,所以是有序的 set 集合 2.Map集合 ConcurrentHashMap是线程安全的哈希表(相当于线程安全的HashMap);它继承于AbstractMap类,并且实现ConcurrentMap接口。 该集合在jdk1.7之前是采用的分段锁机制,将哈希表分为16段,每段是一个锁,每个段又都是哈希表,写入扩容锁定,读取共享,类似于读写锁机制。 在jdk1.8之后,取消分段锁,改用CAS无锁算法,提高写入小路,另外put方法有锁

Is there a C# equivalent to Java's CountDownLatch?

牧云@^-^@ 提交于 2019-11-27 14:32:08
问题 Is there a C# equivalent to Java's CountDownLatch? 回答1: The .NET Framework version 4 includes the new System.Threading.CountdownEvent class. 回答2: Here is a simple implementation (from 9 Reusable Parallel Data Structures and Algorithms): To build a countdown latch, you just initialize its counter to n, and have each subservient task atomically decrement it by one when it finishes, for example by surrounding the decrement operation with a lock or with a call to Interlocked.Decrement. Then,

基于zookeeper实现分布式锁

巧了我就是萌 提交于 2019-11-27 13:31:25
  关于zookeeper的基础知识及安装部署,这位文章已经讲的很清楚了,传送门 https://my.oschina.net/u/3796575/blog/1845035 ,场景为全局id生成,直接说一下思路,有两种实现,一种基于临时节点,一种是基于临时顺序节点,当然共同的部分要用的zookeeper的watch机制以及客户端断开连接后临时节点自动删除的特性。   基于临时节点   依赖 <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency>   简单的序列化 package com.jlwj.zklock.service; import org.I0Itec.zkclient.exception.ZkMarshallingError; import org.I0Itec.zkclient.serialize.ZkSerializer; import java.io.UnsupportedEncodingException; public class MyZkSerializer implements ZkSerializer { private String charset = "UTF-8";

Java并发编程总结4——ConcurrentHashMap在jdk1.8中的改进

冷暖自知 提交于 2019-11-27 13:26:19
Java并发编程总结4——ConcurrentHashMap在jdk1.8中的改进 一、简单回顾ConcurrentHashMap在jdk1.7中的设计 先简单看下ConcurrentHashMap类在jdk1.7中的设计,其基本结构如图所示: 每一个segment都是一个HashEntry<K,V>[] table, table中的每一个元素本质上都是一个HashEntry的单向队列。比如table[3]为首节点,table[3]->next为节点1,之后为节点2,依次类推。 public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K, V>, Serializable { // 将整个hashmap分成几个小的map,每个segment都是一个锁;与hashtable相比,这么设计的目的是对于put, remove等操作,可以减少并发冲突,对 // 不属于同一个片段的节点可以并发操作,大大提高了性能 final Segment<K,V>[] segments; // 本质上Segment类就是一个小的hashmap,里面table数组存储了各个节点的数据,继承了ReentrantLock, 可以作为互拆锁使用 static final class

CountDownLatch源码

做~自己de王妃 提交于 2019-11-27 07:51:19
public class CountDownLatchExample1 { public static void main(String[] args) throws Exception { ExecutorService exec = Executors1.newCachedThreadPool(); final CountDownLatch1 countDownLatch = new CountDownLatch1(3); for (int i = 0; i < 2; i++) { exec.execute(() -> { try { System.out.println("我我我我我我"); } catch (Exception e) { } finally { //state=0(不能向下减了,这个线程继续向下执行完,就不管CountDownLatch了,),减1之后!=0继续向下执行完。 //减1之后=0唤醒队列下一个节点。队列中只有main线程,实例化的线程不会去排队,只会执行完。 countDownLatch.countDown();// 为了保证必须减一,写在finally里面 } }); }//线程在这里已经执行完了, //state=0 main线程就向下执行,state!=0 main线程去排队。 countDownLatch.await(); //

Java并发之:生产者消费者问题

别等时光非礼了梦想. 提交于 2019-11-27 06:48:18
生产者消费者问题是Java并发中的常见问题之一,在实现时,一般可以考虑使用juc包下的BlockingQueue接口,至于具体使用哪个类,则就需要根据具体的使用场景具体分析了。本文主要实现一个生产者消费者的原型,以及实现一个生产者消费者的典型使用场景。 第一个问题:实现一个生产者消费者的原型。 1 import java.util.concurrent.*; 2 3 class Consumer implements Runnable { 4 BlockingQueue q = null; 5 6 public Consumer(BlockingQueue q) { 7 this.q = q; 8 } 9 10 @Override 11 public void run() { 12 while(true) { 13 try { 14 q.take(); 15 System.out.println("Consumer has taken a product."); 16 }catch(InterruptedException e) { 17 18 } 19 } 20 } 21 } 22 23 class Producer implements Runnable { 24 BlockingQueue q = null; 25 26 public Producer