java concurrent并发包使用
package cn.com.zxf.atomic; import java.util.concurrent.atomic.AtomicInteger; public class AtomicExample implements Runnable{ private AtomicInteger atomicInteger; private int index; public AtomicExample(AtomicInteger atomicInteger, int index){ this.atomicInteger=atomicInteger; this.index = index;
} @Override
public void run() { System.out.println("当前线程名称:"+Thread.currentThread().getName()); atomicInteger.addAndGet(index); }
}
package cn.com.zxf.atomic; import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; public class AtomicExampleTest { //线程池
private static ExecutorService executorService = Executors.newFixedThreadPool(10); public static void main(String[] args) throws Exception {
//拦珊 使用
CountDownLatch countDownLatch = new CountDownLatch(100);
//信号量
Semaphore semaphore = new Semaphore(3);
//atomic 包使用 底层是基于CAS 并发编程
final AtomicInteger atomicInteger = new AtomicInteger();
//java 提供的锁
Lock lock = new ReentrantLock();
//队列
LinkedBlockingDeque linkedBlockingDeque = new LinkedBlockingDeque();
for(int i = 0 ;i<100; i++){
Thread.sleep(1000);
semaphore.acquire();
executorService.execute(new AtomicExample(atomicInteger,1));
semaphore.release(); Thread.sleep(1000);
countDownLatch.countDown();
} countDownLatch.await(); executorService.shutdown();
System.out.println("计算值:"+atomicInteger.get());
}
}
concurrent 使用
java concurrent并发包使用的更多相关文章
- java.util.concurrent并发包诸类概览
java.util.concurrent包的类都来自于JSR-166:Concurrent Utilities,官方的描述叫做“The JSR proposes a set of medium-lev ...
- Java集合及concurrent并发包总结(转)
Java集合及concurrent并发包总结(转) 1.集合包 集合包最常用的有Collection和Map两个接口的实现类,Colleciton用于存放多个单对象,Map用于存放Key-Valu ...
- Java并发:多线程和java.util.concurrent并发包总结
多线程和java.util.concurrent并发包 转载:
- Java并发—java.util.concurrent并发包概括(转载)
一.描述线程的类:Runable和Thread都属于java.lang包 二.内置锁synchronized属于jvm关键字,内置条件队列操作接口Object.wait()/notify()/noti ...
- java concurrent包的学习(转)
java concurrent包的学习(转) http://my.oschina.net/adwangxiao/blog/110188 我们都知道,在JDK1.5之前,Java中要进行业务并发时,通常 ...
- How to Create a Java Concurrent Program
In this Document Goal Solution Overview Steps in writing Java Concurrent Program Template ...
- Java Concurrent Topics
To prevent Memory Consistency Errors(MCEs), it is good practice to specify synchronized class specif ...
- WebDav的java客户端开发包:Jackrabbit
上一篇帖子“WebDav的java客户端开发包:sardine”中说到,对于开发WebDav客户端 sardine是一个很好的选择,但sardine并未实现WevDav的全部规范,所以我又试了试 ap ...
- WebDav的java客户端开发包:sardine
最近需要对WebDav服务器进行操作,查找了一下,基于java的开发包主要有这几个: slide Jackrabbit sardine webdavclient4j 其中slide是apache的一个 ...
随机推荐
- 基于变分自编码器(VAE)利用重建概率的异常检测
本文为博主翻译自:Jinwon的Variational Autoencoder based Anomaly Detection using Reconstruction Probability,如侵立 ...
- Maven的下载以及配置
Maven的下载以及配置 Maven的两大核心作用: (1)依赖管理:对Jar包的依赖,解决Jar包之间的冲突 (2)项目构建:项目从编译到测试到运行发布 一.Mavenu的下载(现在的eclipse ...
- python文件读取,替换(带格式,python lib 库)
import os, time import sys import re def read_old_part(filename, start, end): content = [] recording ...
- Simple Redux
This is a post that tries to explain the the basics of Redux. We’ll build a minimal working example ...
- swift是面向对象、面向协议、高阶类型、灵活扩展、函数式编程语言
swift是面向对象.面向协议.高阶类型.灵活扩展.函数式编程语言
- 使用solr将CSV/XML/DB/JSON数据发布为Rest Service
Download http://lucene.apache.org/solr/downloads.html Apache Solr入门基础——Windows下安装与配置 https://blog.cs ...
- CLR Exception---E0434352
什么是CLR Exception---E0434352 CLR异常是.NET应用程序生成的异常类型.异常被封装在从System.exception类派生的类中.它的异常代码是0xE0434352,代码 ...
- ent 基本使用十五 一个图遍历的例子
以下是来自官方的一个user group pet 的查询demo 参考关系图 环境准备 docker-compose mysql 环境 version: "3" services: ...
- 14-网页,网站,微信公众号基础入门(网页版MQTT,小试牛刀)
https://www.cnblogs.com/yangfengwu/p/11192639.html 抱歉哈...最近由于做板子,,教程的进度落下了... 这些天总共做了还几块板子 首先对当前这个教程 ...
- GitHub上如何创建文件夹
看了网上很多关于如何在git上创建空文件夹的文章后,发现大家写的都是用指令在本地创建一个空文件夹后再上传指令和步骤都太繁琐且复杂了,对于用git不是很熟练得到人来说太麻烦了,而且在本地于github上 ...