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的一个 ...
随机推荐
- c语言实现基本的数据结构(四) 循环队列
#include <stdio.h> #include <tchar.h> #include <stdlib.h> #define MaxQueueSize 100 ...
- Android Studio在代码重构中的妙用
代码重构几乎是每个程序员在软件开发中必须要不断去做的事情,以此来不断提高代码的质量.Android Stido(以下简称AS)以其强大的功能,成为当下Android开发工程师最受欢迎的开发工具,也是A ...
- MyBatis-plus的入门学习
MyBatis优势: Sql简单语句可以自由控制,更灵活,性能更高.. sql与代码分离,易于阅读和维护 提供xml标签,支持编写动态sql语句. 劣势: 简单crud操作还是写SQL 语句 xml中 ...
- ArcGIS for Server 10.2 发布Feature Service
折腾一下午,终于把自带的例子发布成Feature Service了,这样就可以通过web编辑了.记录一下步骤. 环境:已经安装好SQL Server 2008 R2,ArcGIS for Deskto ...
- Python基础知识笔记-作用域
Python 中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量是在哪里赋值的. 变量的作用域决定了在哪一部分程序可以访问哪个特定的变量名称.Python的作用域一共有4种,分别是: ...
- Gym100739H Hard Molecules
Hard Molecules 给定一个连通图中每个点的度数,求一个满足条件的图,图可以有重边,不能有自环. n<=5000, di<=109 题解 如果不要求图连通,那么只需要判断 \[ ...
- ajax jsonp函数调用
jsonp数据 jsonpHandler({name:"liujinyu",age:"24"}) ajax调用 $.ajax({ type:'GET', ...
- template-web.js 自定义过滤器
// 比如需要自定义一个去零的过滤器 <script id="templateTest" type="text/html"> <% for(i ...
- 开发(二) ardunio批量固件上传地址
https://blog.csdn.net/Naisu_kun/article/details/84958561 批量烧录固件到模块中上面讲了如何编写上传程序,接下来讲讲如何量产.相比<Ardu ...
- dotnetcore docker 简单运行
今天试用了下mac 版本的dotnetcore sdk,发现还是很方便的,同时官方的容器运行方式,相对小了好多 同时使用多阶段构建的方式运行dotnetcore 安装sdk 下载地址: https:/ ...