Java并发编程之——Amino框架
Amino框架是一个采用无锁方式实现并行计算的框架,可惜的是,网上关于Amino框架的介绍甚少。根据所掌握的资料,稍微总结一下:
1. 锁机制到无锁机制
2. 基于Compare-and-swap(CAS) 算法的无锁并发控制方法
- public final int getAndSet(int newValue){
- for(;;){
- int current=get();
- if(compareAndSet(current,newValue))
- return current;
- }
- }
3. 引出Amino框架
4. 性能测试
- public class CopyList {
- public static void test1(AccessListTread t, String name){
- CounterPoolExecutor exe0=new CounterPoolExecutor(2000,2000,0L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
- exe0.TASK_COUNT=8000;
- exe0.funcname=name;
- exe0.startTime=System.currentTimeMillis();
- for(int i=0;i<exe0.TASK_COUNT;i++)
- exe0.submit(t);//测试数据:8000
- exe0.shutdown();
- }
- public static void main(String[] args) {
- AccessListTread t=new AccessListTread();
- t.initCopyOnWriteArrayList();
- test1(t,"testCopyOnWriteArrayList");
- t.initVector();
- test1(t,"testVector");
- t.initLockFreeList();
- test1(t,"testLockFreeList");
- t.initLockFreeVector();
- test1(t,"testLockFreeVector");
- }
- }
- class CounterPoolExecutor extends ThreadPoolExecutor{
- public AtomicInteger count=new AtomicInteger(0);//统计次数
- public long startTime=0;
- public String funcname="";
- public int TASK_COUNT=0;
- public CounterPoolExecutor(int corePoolSize, int maximumPoolSize,
- long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
- super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
- }
- @Override
- protected void afterExecute(Runnable r,Throwable t){
- int l=count.addAndGet(1);
- if(l==TASK_COUNT){
- System.out.println(funcname+"spend time:"+(System.currentTimeMillis()-startTime));
- }
- }
- }
- class AccessListTread implements Runnable{
- Random rand=new Random();
- List list;
- public AccessListTread() {
- }
- @Override
- public void run() {
- try {
- for(int i=0;i<1000;i++)
- // getList(rand.nextInt(1000));
- handleList(rand.nextInt(1000));
- Thread.sleep(rand.nextInt(100));
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- private Object getList(int nextInt) {
- return list.get(nextInt);
- }
- private Object handleList(int index) {
- list.add(index);
- list.remove(index%list.size());
- return null;
- }
- //test
- public void initCopyOnWriteArrayList(){
- list=new CopyOnWriteArrayList();
- for(int i=0;i<1000;i++)
- list.add(i);
- }
- public void initVector(){
- list=new Vector();
- for(int i=0;i<1000;i++)
- list.add(i);
- }
- public void initLockFreeList(){
- list=new LockFreeList();
- for(int i=0;i<1000;i++)
- list.add(i);
- }
- public void initLockFreeVector(){
- list=new LockFreeVector();
- for(int i=0;i<1000;i++)
- list.add(i);
- }
- }
测试结果:
- testVectorspend time:366
- testCopyOnWriteArrayListspend time:717
- testLockFreeListspend time:541
- testLockFreeVectorspend time:244
Java并发编程之——Amino框架的更多相关文章
- Java 并发编程 -- Fork/Join 框架
概述 Fork/Join 框架是 Java7 提供的一个用于并行执行任务的框架,是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架.下图是网上流传的 Fork Join 的 ...
- Java并发编程--Fork/Join框架使用
上篇博客我们介绍了通过CyclicBarrier使线程同步,可是上述方法存在一个问题,那就是假设一个大任务跑了2个线程去完毕.假设线程2耗时比线程1多2倍.线程1完毕后必须等待线程2完毕.等待的过程线 ...
- Java并发编程--4.Executor框架
简介 Executor框架是启动,管理线程的API, 它的内部实现是线程池机制,它有很多好处,比如使任务提交和任务执行解耦合,防止this逃逸:它的主要API包括: Executor, Execut ...
- java并发编程-Executor框架
Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,Completion ...
- Java 并发编程——Executor框架和线程池原理
Eexecutor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程相当于生产者,执行任务 ...
- 那些年读过的书《Java并发编程实战》和《Java并发编程的艺术》三、任务执行框架—Executor框架小结
<Java并发编程实战>和<Java并发编程的艺术> Executor框架小结 1.在线程中如何执行任务 (1)任务执行目标: 在正常负载情况下,服务器应用 ...
- (转)java并发编程--Executor框架
本文转自https://www.cnblogs.com/MOBIN/p/5436482.html java并发编程--Executor框架 只要用到线程,就可以使用executor.,在开发中如果需要 ...
- Java并发编程原理与实战三十二:ForkJoin框架详解
1.Fork/Join框架有什么用呢? ------->Fork使用来切分任务,Join是用来汇总结果.举个简单的栗子:任务是1+2+3+...+100这个任务(当然这个任务的结果有好的算法去做 ...
- Java 并发编程——Executor框架和线程池原理
Java 并发编程系列文章 Java 并发基础——线程安全性 Java 并发编程——Callable+Future+FutureTask java 并发编程——Thread 源码重新学习 java并发 ...
随机推荐
- 每天一个linux命令(文件操作):【转载】find 命令的参数详解
find一些常用参数的一些常用实例和一些具体用法及注意事项. 1.使用name选项: 文件名选项是find命令最常用的选项,要么单独使用该选项,要么和其他选项一起使用.可以使用某种文件名模式来匹配文件 ...
- java并发--CountDownLatch、CyclicBarrier和Semaphore
在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法. 以下 ...
- Windows10安装MySQL8.0
1.到MySQL官网下载安装包:https://dev.mysql.com/downloads/mysql/:选择8.0版本: 2.将下载好的安装包(mysql-8.0.12-winx64 .zip) ...
- Let’s Encrypt 将于 2018 年免费提供通配符证书
旨在让每个网站都启用 HTTPS 加密的 Let's Encrypt CA 宣布将于 2018 年 1 月免费提供通配符证书(Wildcard certificate).通配符证书是一种可被多个子域使 ...
- oracle之 RAC Interconnect之HAIP
0. 背景 Oracle 从11.2.0.2开始引入了一个新特性叫做Redundant Interconnect,简称HAIP.HAIP的目的用来代替操作系统级别的网卡绑定以实现Active-Acti ...
- git 获取远程分支
另一哥们将分支push到库中,我怎么获取到他的分支信息呢? 如果安装了git客户端,直接选择fetch一下,就可以获取到了. 如果用命令行,运行 git fetch,可以将远程分支信息获取到本地,再运 ...
- requireJS多页面应用实例
本文是requireJS的一些知识点的总结,配上多页面应用中的实例分析. 本案例的目录结构如下: requireJS API的三个主要函数:define(创建模块),require(加载模块),con ...
- Extjs tree1
1.代码如下: 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- Netty--JDK序列化编解码传输对象
使用JDK序列化不需要额外的类库,只需要实现Serializable即可,但是序列化之后的码流只有Java才能反序列化,所以它不是跨语言的,另外由于Java序列化后码流比较大,效率也不高,所以在RPC ...
- windows 我永远的最爱
我配置好这个真不容易.总结下,配置中没搞清楚发布远程日志网址的意思:每一个博客配置都不同,比如新浪.网易.51技术博客