countdownlatch

OutOfMemoryError: unable to create new native t...

天大地大妈咪最大 提交于 2019-12-03 16:15:57
一、认识问题: 首先我们通过下面这个 测试程序 来认识这个问题: 运行的环境 (有必要说明一下,不同环境会有不同的结果):32位 Windows XP,Sun JDK 1.6.0_18, eclipse 3.4, 测试程序: import java.util.concurrent.CountDownLatch; public class TestNativeOutOfMemoryError { public static void main(String[] args) { for (int i = 0;; i++) { System.out.println("i = " + i); new Thread(new HoldThread()).start(); } } } class HoldThread extends Thread { CountDownLatch cdl = new CountDownLatch(1); public HoldThread() { this.setDaemon(true); } public void run() { try { cdl.await(); } catch (InterruptedException e) { } } } 不指定任何JVM参数,eclipse中直接运行输出,看到了这位朋友了吧: i = 5602 Exception

CountDownLatch使用及实现原理

我与影子孤独终老i 提交于 2019-12-03 16:05:18
CountDownLatch是Java提供的线程递减锁,为开发人员封装出来针对特殊场景使用的工具类,主要用在某一个动作的执行以来其他动作对应线程的完成。 使用步骤: 1.拿到一个CountDownLatch实例,初始化大小为依赖的动作线程数 2.将CountDownLatch实例传递给依赖线程 3.在依赖线程执行完对应的动作后调用CountDownLatch.countDown方法 4.在主线程中调用CountDownLatch.await方法 package com.jv.parallel.feature; import java.util.concurrent.CountDownLatch; /* * 线程递减锁,用来控制某一个动作的执行依赖其他动作线程的完成 * * 例如:开始钓鱼之前先要做主线、拌饵料、打窝子 */ public class TestCountDownLatch { public static void main(String[] args) throws InterruptedException { // 1.声明一个线程递减锁 // 因为在钓鱼 CountDownLatch cdl = new CountDownLatch(3); new Thread(new TieLine(cdl)).start(); new Thread(new MixedFood

Java多线程(十五):CountDownLatch,Semaphore,Exchanger,CyclicBarrier,Callable和Future

你说的曾经没有我的故事 提交于 2019-12-03 11:20:43
摘自: https://www.cnblogs.com/Java-Starter/p/11570249.html Java多线程(十五):CountDownLatch,Semaphore,Exchanger,CyclicBarrier,Callable和Future 技术讨论QQ群:135202158 目录 CountDownLatch Semaphore Exchanger CyclicBarrier Callable和Future 正文 回到顶部 CountDownLatch CountDownLatch用来使一个线程或多个线程等待到其他线程完成。CountDownLatch有个初始值count,await方法会阻塞线程,直到通过countDown方法调用使count减少为0才会执行await方法后面的代码。 示例代码 MyThread50_0是WorkThread,不同的线程休眠时间不一样。 public class MyThread50_0 extends Thread { private CountDownLatch cdl; private int sleepSecond; public MyThread50_0(String name, CountDownLatch cdl, int sleepSecond) { super(name); this.cdl = cdl

线程的相关技术总结

荒凉一梦 提交于 2019-12-03 10:46:43
--CountDownKatch CountDownLatch 内部维护了一个整数 n ,n(要大于等于0)在 当前线程 初始化 CountDownLatch 方法指定。当前线程调用 CountDownLatch 的 await() 方法阻塞当前线程,等待其他调用 CountDownLatch 对象的 CountDown() 方法的线程执行完毕。 其他线程调用该 CountDownLatch 的 CountDown() 方法,该方法会把 n-1 ,直到所有线程执行完成, n 等于 0 ,当前线程 就恢复执行 --CyclicBarrier CyclicBarrier 是一个同步辅助类,允许一组线程互相等待,直到到达某个公共屏障点(CommonBarrierPoint)。因为该 barrier 在释放等待线程后可以重用,所以称它为循环的 barrier 。 -- Semaphore Semaphore 直译为信号。实际上 Semaphore 可以看做是一个信号的集合。不同的线程能够从 Semaphore 中获取若干个信号量。当 Semaphore 对象持有的信号量不足时,尝试从 Semaphore 中获取信号的线程将会阻塞。直到其他线程将信号量释放以后,阻塞的线程会被唤醒,重新尝试获取信号量。 --说说 CountDownLatch 与 CyclicBarrier 区别

Resettable CountdownLatch

偶尔善良 提交于 2019-12-03 10:07:56
I need something which is directly equivalent to CountDownLatch , but is resettable (remaining thread-safe!). I can't use classic synchronisation constructs as they simply don't work in this situation (complex locking issues). At the moment, I'm creating many CountDownLatch objects, each replacing the previous one. I believe this is doing in the young generation in the GC (due to the sheer number of objects). You can see the code which uses the latches below (it's part of the java.net mock for a ns-3 network simulator interface). Some ideas might be to try CyclicBarrier (JDK5+) or Phaser (JDK7

Java support for three different concurrency models

一笑奈何 提交于 2019-12-03 04:52:26
问题 I am going through different concurrency model in multi-threading environment (http://tutorials.jenkov.com/java-concurrency/concurrency-models.html) The article highlights about three concurrency models . Parallel Workers The first concurrency model is what I call the parallel worker model. Incoming jobs are assigned to different workers . Assembly Line The workers are organized like workers at an assembly line in a factory. Each worker only performs a part of the full job. When that part is

CountDownLatch和CycliBarrier介绍

六月ゝ 毕业季﹏ 提交于 2019-12-03 03:58:01
一、CountDownLatch   它被用来同步一个或多个任务,强制他们等待其他任务完成,这就是闭锁。 public CountDownLatch(int count) { if (count < 0) throw new IllegalArgumentException("count < 0"); this.sync = new Sync(count); }   类中只有一个构造函数,一个int类型的参数count,代表计数器。这个计数器的初始值是线程的数量,每当一个线程结束,count-1,当count==0 时,所有线程执行完毕,在闭锁上等待的线程就可以执行了。 类中还包含了三个公共方法: public void await() public boolean await(long timeout, TimeUnit unit) public void countDown()   当每个任务完成时,都会调用countDown()方法。而等待问题被解决的任务在这个锁存器上调用await()方法,这个任务就相当于被挂起了直到 timeout 或 计数器为0 。 public class CountDownLatchDemo { static final int SIZE = 5; public static void main(String[] args) {

Android AsyncTask testing with Android Test Framework

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a very simple AsyncTask implementation example and am having problem in testing it using Android JUnit framework. It works just fine when I instantiate and execute it in normal application. However when it's executed from any of Android Testing framework classes (i.e. AndroidTestCase , ActivityUnitTestCase , ActivityInstrumentationTestCase2 etc) it behaves strangely: It executes doInBackground() method correctly However it doesn't invokes any of its notification methods ( onPostExecute() , onProgressUpdate() , etc) -- just

使用Redlock实现分布式锁

匿名 (未验证) 提交于 2019-12-03 00:26:01
之前写过一篇文章 《如何在springcloud分布式系统中实现分布式锁?》 ,由于自己仅仅是阅读了相关的书籍,和查阅了相关的资料,就认为那样的是可行的。那篇文章实现的大概思路是用setNx命令和setEx配合使用。 setNx是一个耗时操作,因为它需要查询这个键是否存在,就算redis的百万的qps,在高并发的场景下,这种操作也是有问题的。关于redis实现分布式锁,redis官方推荐使用redlock。 一、redlock简介 在不同进程需要互斥地访问共享资源时,分布式锁是一种非常有用的技术手段。实现高效的分布式锁有三个属性需要考虑: 安全属性:互斥,不管什么时候,只有一个客户端持有锁 效率属性A:不会死锁 效率属性B:容错,只要大多数redis节点能够正常工作,客户端端都能获取和释放锁。 Redlock是redis官方提出的实现分布式锁管理器的算法。这个算法会比一般的普通方法更加安全可靠。关于这个算法的讨论可以看下 官方文档 。 二、怎么用java使用 redlock 在pom文件引入redis和redisson依赖: <!-- redis--> < dependency > < groupId > org.springframework.boot </ groupId > < artifactId > spring-boot-starter-data-redis </

多线程并发访问之 Semaphore、CountDownLatch

匿名 (未验证) 提交于 2019-12-03 00:22:01
今天业务需求开发需要开发一个洗数据的小功能,大致业务是有百万级别的数据需要清洗,需要开发一个小功能,循环遍历百万数据调用一个服务接口清晰数据。考虑到接口的并发量,访问量不能太大, 整了一两个小时做了一个并发控制的小程序。 public static class CleanTask implements Runnable { private static Semaphore semaphore = new Semaphore( 5 , true ) ; private Runnable runnable ; private CountDownLatch latch ; public CleanTask (CountDownLatch latch , Runnable runnable) { this . runnable = runnable ; this . latch = latch ; } @Override public void run () { try { semaphore .acquire() ; runnable .run() ; semaphore .release() ; } catch (InterruptedException ex) { LOG .error( "线程中断异常" , ex) ; } finally { latch .countDown()