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的一个 ...
随机推荐
- 洛谷 P1162 填涂颜色题解
题目描述 由数字00组成的方阵中,有一任意形状闭合圈,闭合圈由数字11构成,围圈时只走上下左右44个方向.现要求把闭合圈内的所有空间都填写成22.例如:6 \times 66×6的方阵(n=6n=6) ...
- php7中的dirname,intdiv,define
<?php //dirname可指定目录级数 //intdiv整数整除 //define可以定义数组 echo dirname('/var/www/html/app/etc/config'); ...
- Fiddler抓websocket协议的包,用jmeter做并发测试
1.Fiddler: 左边为ws请求url.右边为请求数据,响应数据 jmeter:
- 怎么查看一个进程里fork多少个子进程
怎么查看一个进程里fork多少个子进程 怎么查看一个进程里fork多少个子进程 怎么查看一个进程里fork多少个子进程 ? ? ?
- JSR303/JSR-349,hibernate validation,spring validation 之间的关系
JSR303是一项标准,JSR-349是其的升级版本,添加了一些新特性,他们规定一些校验规范即校验注解,如@Null,@NotNull,@Pattern,他们位于javax.validation.co ...
- w5500驱动使用方法调试笔记
1.w5500有两种方式可以运行的,server端一般用中断的方式,效率比较高,client使用查询的模式,本身自带2k的发送缓存和2k的接收缓存.2.查询模式:有数据的时候,查询模式可以每隔一段时间 ...
- LeetCode 911. Online Election
原题链接在这里:https://leetcode.com/problems/online-election/ 题目: In an election, the i-th vote was cast fo ...
- MongoDB 模糊查询like
1.LIKE模糊查询userName包含A字母的数据(%A%)-- SQL:SELECT * FROM UserInfo WHERE userName LIKE "%A%" -- ...
- Windows 10中的CSC.exe、CSC.rsp
(1)CSC.exe位置 [4.0的位于] C:\Windows\Microsoft.NET\Framework\v4.0.30319 [之后版本的位于] C:\Program Files (x86) ...
- Spring通知,顾问,增强
1.AOP (Aspect Oriented Programming 面向切面编程) 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编 ...