一直被忽略的java 多线程并发工具类 CountDownLatch、Semaphore
来源:互联网 发布:c4d r17 mac注册机 编辑:程序博客网 时间:2024/06/05 18:57
CountDownLatch: 进行等待,知道发生指定数量事件后停止
构造方法:CountDownLatch(int num)
num是指锁存器而必须发生的事件次数。
主要方法:
await() : 当计数器(也就是上面的num)等于0时才结束等待
countDown(): 计数器(num)递减
Semaphore: 实现经典信号量
构造方法:Semaphore(int num)
num是产生的信号量个数
主要方法:
acquire(): 获取一个信号量
release(): 释放一个信号量
代码如下:
package com.test;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Semaphore;public class Question2 implements Runnable{ private String name; private Semaphore sem; private CountDownLatch downLatch; public Question2(String name, Semaphore sem,CountDownLatch downLatch) { super(); this.name = name; this.sem = sem; this.downLatch = downLatch; } public void test(String name){ System.out.println(name + " in!"); try { Thread.sleep(2 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(name + " out!"); } public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub int thread_num = 10; ExecutorService executorService = Executors.newCachedThreadPool(); Semaphore sem = new Semaphore(1); //每次产生信号量个数 CountDownLatch downLatch = new CountDownLatch(5); //初始化同步器 for(int i=0; i< thread_num;i++){ executorService.execute(new Question2("test "+i, sem,downLatch)); } downLatch.await(); executorService.shutdown(); } @Override public void run() { // TODO Auto-generated method stub try { sem.acquire(); this.test(this.name); sem.release(); downLatch.countDown(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
阅读全文
0 0
- 一直被忽略的java 多线程并发工具类 CountDownLatch、Semaphore
- JAVA多线程系列--并发工具类(CountDownLatch, CyclicBarrier, Semaphore,Exchanger)
- Java线程(CountDownLatch、CyclicBarrier、Semaphore)并发控制工具类
- Java并发编程之2——同步工具类的使用(CountDownLatch,CyclicBarrier,BlockungQueue,Semaphore)
- Java并发-同步器Semaphore,CountDownLatch
- JAVA并发CountDownLatch、CyclicBarrier、Semaphore
- 第8章 Java中的并发工具类(CountDownLatch CyclicBarrier Semaphore Exchanger)
- 四个并发工具类CountDownLatch,CyclicBarrier,Semaphore,Exchanger
- 四个并发工具类CountDownLatch,CyclicBarrier,Semaphore,Exchanger
- Java并发工具类CountDownLatch
- java 并发工具类-CountDownLatch
- Java并发工具类CountDownLatch
- java 并发工具类-Semaphore
- 并发工具类(一)等待多线程完成的CountDownLatch
- 并发工具类(一)等待多线程完成的CountDownLatch
- java 并发类semaphore countdownlatch cyclicbarrier reentrantlock condition reentrantreadwritelock
- java 多线程同步辅助类(CountDownLatch、CyclicBarrier、Semaphore、Exchanger)
- Java并发之CountDownLatch、CyclicBarrier、Semaphore
- python selenium2 中的显示等待WebDriverWait与条件判断expected_conditions举例
- IntelliJ 创建main函数快捷
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor 【位运算+思维】
- react-native统一入口界面,并实现页面的跳转
- 打印机慢
- 一直被忽略的java 多线程并发工具类 CountDownLatch、Semaphore
- Xamarin.IOS Binding Objective Sharpie
- ext修改行颜色,隔行颜色没变解决方案
- PHP Reflection API详解
- SLAM学习资料整理
- require() 源码解读
- MediaStore.Images.Media.insertImage
- 欢迎使用CSDN-markdown编辑器
- C语言读书笔记