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自学(一)之vmware虚拟机安装
之前有研究过linux,后来一段时间没有操作了,现在有点陌生,而且当初也没有记录学习内容.现在想从新开始包括虚拟机安装到部署Javaweb项目,把这之间所需要的全都记录下来,以便后边学习参考使用. 虚 ...
- 当我们使用 MVVM 模式时,我们究竟在每一层里做些什么?
这篇文章不会说 MVVM 是什么,因为讲这个的文章太多了:也不会说 MVVM 的好处,因为这样的文章也是一搜一大把.我只是想说说我们究竟应该如何理解 M-V-VM,当我们真正开始写代码时,应该在里面的 ...
- Java8中计算日期时间差
一.简述 在Java8中,我们可以使用以下类来计算日期时间差异: 1.Period 2.Duration 3.ChronoUnit 二.Period类 主要是Period类方法getYears(),g ...
- c++ template不能有cpp
c++的template只能把生命和定义都放在.h文件里,不然会出错
- Apache Tez 了解
你可能听说过Apache Tez,它是一个针对Hadoop数据处理应用程序的新分布式执行框架.但是它到底是什么呢?它的工作原理是什么?哪些人应该使用它,为什么?如果你有这些疑问,那么可以看一下Bika ...
- Linux环境下Maven的.m2文件夹
aven中的.m2文件夹 安装完maven是没有.m2文件夹的.在linux中以.开头的文件夹都是隐藏的.当使用maven命令的时候,maven自动会创建.m2文件夹. 运行命令mvn help:sy ...
- Docker Toolbox常见错误解决方案
错误1 Error checking TLS connection: Error checking and/or regenerating the certs: There was an error ...
- 016:Explain
一. Explain EXPLAIN 官方文档 1.explain说明 explain是解释SQL语句的执行计划,即显示该SQL语句怎么执行的 使用explain的时候,也可以使用desc 5.6 版 ...
- django随机验证码
Python生成随机验证码,需要使用PIL模块. 安装: 1 python3.5 -m pip install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 from P ...
- STL sort
STL的sort()算法,数据量大时采用Quick Sort,分段递归排序,一旦分段后的数据量小于某个门槛,为避免Quick Sort的递归调用带来过大的额外负荷,就改用Insertion Sort. ...