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并发 ...
随机推荐
- const in C/C++
1.const 对象必须初始化,因为一旦创建后值不能改变. Eg: const int i = GetSize(); //正确:运行时初始化 const int j = 42; //正确:编译时初 ...
- 【模板】NTT
NTT模板 #include<bits/stdc++.h> using namespace std; #define LL long long const int MAXL=22; con ...
- 像黑客一样!Chrome 完全键盘操作指南(原生快捷键 + Vimium 插件)
有那么一波小伙伴,多数时候都不需要用到鼠标,通常他们正好是“黑客”.当你开始使用键盘操作一切时,便能体会到无需用鼠标瞄准按钮时的干脆,无需在键盘和鼠标之间移动手时的轻松. Chrome 原生自带大量快 ...
- apiman 一个apigateway
APIMAN 提供 API 管理的方法技术,结合一个 API 设计/配置层以及快速的运行时. 主要特性: 完全异步 丰富的管理层 容易嵌入 API 管理 参考资料 http://www.apiman. ...
- Helm Charts
Use this repository to submit official Charts for Kubernetes Helm. Charts are curated application de ...
- Oracle冷备份和热备份的实践(原创)
参考本博转发的备份博文和上传的文件,进行了冷热备份实践并进行了记载以备以后查阅,本次实践的环境是win10,安装了oracle11g 一.冷备份 1.cmd->sqlplus /nolog 2. ...
- xunsearch的使用(二)
1.查看配置文件vim /data/local/xunsearch/sdk/php/app/demo.ini [pid] type = id [subject] type = title [messa ...
- celery制作定时任务
celery参考地址:http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#starting-the-schedu ...
- 怎么使用ping命令进行连通性测试
关于ping命令的作用: ping 命令有助于验证网络层的连通性!一般进行网络故障排除时,可以使用ping 命令向目标计算机或IP地址发送ICMP回显请求,目标计算机会返回回显应答,如果目标计算机不能 ...
- java web 程序---刷新页面次数进一步
<%@ page language="java" import="java.util.*" pageEncoding="gb2312" ...