CAS实现无锁模式
用多线程实现一个数字的自增长到1000000,分别用无锁模式和锁模式来实现代码.
1.使用ReentrantLock.
package test; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock; public class ThreadWithLock { private static final int THREAD_COUNT=3;
private static volatile int value= 0;
private static final ReentrantLock lock = new ReentrantLock();
private static final CountDownLatch startSignal = new CountDownLatch(1);
private static final CountDownLatch endSignal = new CountDownLatch(1); public static void main(String[] args) throws InterruptedException{ for(int i=0;i<=THREAD_COUNT;i++){
(new Counter()).start();
}
//start to record time.
long start = System.nanoTime();
//send the started signal to all threads.
startSignal.countDown();
//wait for anyone to finish the counting.
endSignal.await();
long end = System.nanoTime();
System.out.println(end-start);
} static class Counter extends Thread { @Override
public void run() {
try {
startSignal.await();
while(true){
try {
lock.lock();
if(value<1000000){
value++;
}
else {
break;
}
}
finally{
lock.unlock();
}
}
endSignal.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
}
使用CAS模式,这其实是一种乐观锁模式,它默认是没有竞争的,如果存在竞争,失败了让代码重试.
package test; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger; public class ThreadNoLock { private static final int THREAD_COUNT=3;
private static final AtomicInteger value= new AtomicInteger(0);
private static final CountDownLatch startSignal = new CountDownLatch(1);
private static final CountDownLatch endSignal = new CountDownLatch(1); public static void main(String[] args) throws InterruptedException{
for(int i=0;i<THREAD_COUNT;i++){
(new Counter()).start();
}
//start to record time.
long start = System.nanoTime();
//send the started signal to all threads.
startSignal.countDown();
//wait for anyone to finish the counting.
endSignal.await();
long end = System.nanoTime();
System.out.println(end-start);
} static class Counter extends Thread { @Override
public void run() {
try {
startSignal.await();
int current;
while(true){
if((current = value.get()) <1000000){
value.compareAndSet(current, current+1);
}
else{
break;
}
}
endSignal.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
}
把Thread_count分别设置为1000,300,30,3 得到的性能数据如下,我们可以发现无锁模式的效率更高.

CAS实现无锁模式的更多相关文章
- 无锁模式的Vector
这两天学习无锁的并发模式,发现相比于传统的 同步加锁相比,有两点好处1.无锁 模式 相比于 传统的 同步加锁 提高了性能 2.无锁模式 是天然的死锁免疫 下来介绍无锁的Vector--- LockF ...
- 使用CAS实现无锁的SkipList
无锁 并发环境下最常用的同步手段是互斥锁和读写锁,例如pthread_mutex和pthread_readwrite_lock,常用的范式为: void ConcurrencyOperation() ...
- CAS无锁模式
一.java内存模型:JMM 在内存模型当中定义一个主内存,所有声明的实例变量都存在于主内存当中,主内存的数据会共享给所有线程,每一个线程有一个块工作内存,工作内存当中主内存数据的副本当更新数据时,会 ...
- 使用CAS实现无锁列队-链表
#include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <iostream& ...
- 基于CAS实现无锁结构
杨乾成 2017310500302 一.题目要求 基于CAS(Compare and Swap)实现一个无锁结构,可考虑queue,stack,hashmap,freelist等. 能够支持多个线程同 ...
- CAS 与 无锁队列
http://coolshell.cn/articles/8239.html http://www.tuicool.com/articles/VZ3IBv http://blog.csdn.net/r ...
- CAS无锁机制原理
原子类 java.util.concurrent.atomic包:原子类的小工具包,支持在单个变量上解除锁的线程安全编程 原子变量类相当于一种泛化的 volatile 变量,能够支持原子的和有条件的读 ...
- 锁、CAS操作和无锁队列的实现
https://blog.csdn.net/yishizuofei/article/details/78353722 锁的机制 锁和人很像,有的人乐观,总会想到好的一方面,所以只要越努力,就会越幸运: ...
- CAS原子操作实现无锁及性能分析
CAS原子操作实现无锁及性能分析 Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn.net/chen19870707 ...
随机推荐
- meta标签的用法
meta是用来在HTML文档中模拟HTTP协议的响应头报文.meta 标签用于网页的<head>与</head>中,meta 标签的用处很多.meta 的属性有两种:name和 ...
- replaceCharactersInRange
NSString 替换字符串中某一位置的文字 replaceCharactersInRange NSString 替换字符串中某一位置的文字 - (void)viewDidLoad { NSMuta ...
- Solve Error Debug Assertion Failed Expression vector iterators incompatible Using PCL in Release Mode of VS2010
When using PCL 1.4.0 in the release mode building under VS2010, we might sometime get the error &quo ...
- Spark - RDD(弹性分布式数据集)
org.apache.spark.rddRDDabstract class RDD[T] extends Serializable with Logging A Resilient Distribut ...
- Java SSH库使用简介:Apache sshd和JSch(Java Secure Channel)
1.Apache sshd Apache sshd是一个SSH协议的100%纯Java库,支持客户端和服务器.sshd库基于Apache MINA项目(可伸缩高性能的异步IO库). 官方网站:http ...
- storm在windows系统下安装调试
基础知识 Storm是一个分布式的,可靠的,容错的数据流处理系统.它会把工作任务委托给不同类型的组件,每个组件负责处理一项简单特定的任务.Storm集群的输入流由一个被称作spout的组件管理,spo ...
- Frenetic HelloSDNWorld
Follow Frenetic-Github HelloSDNWorld 实验环境: Frenetic虚拟机: 实验步骤: 1.Start up a terminal window - – two a ...
- Yii源码阅读笔记(八)
前面阅读了Yii2的两个基本类Object和Component,了解了Yii的三个重要概念属性.事件.行为,下面开始阅读Event类,Event类是所有事件类的基类: <?php /** * @ ...
- Irrlicht引擎I 配置
游戏是一个比较大的系统,包含了图形引擎.网络.AI.声音.UI等模块,模块的开发可能会分别进行或者采用开源项目,Irrlicht引擎基本包含了这些模块,不过在使用中也会陆续加入其它的模块.以前开发的程 ...
- n0_n1
#include<stdio.h>int a[10];void quanpailie(int i){ if(i==10) {for(i=0;i<10;i++) { print ...