《Java并发编程实战》第十五章 原子变量与非堵塞同步机制 读书笔记
一、锁的劣势
二、硬件对并发的支持
1 比較并交换
CAS是一项乐观锁技术。
@ ThreadSafe
public class SimulatedCAS {
@ GuardeBy( "this") private int value ; public synchronized int get(){
return value ;
} public synchronized int compareAndSwap( int expectedValue, int newValue){
int oldValue = value ;
if (oldValue == expectedValue)
value = newValue;
return oldValue;
} public synchronized boolean compareAndSet( int expectedValue, int newValue){
return (expectedValue == compareAndSwap(expectedValue, newValue));
}
}
@ ThreadSafe
public class CasCounter {
private SimulatedCAS value ; public int getValue(){
return value .get();
} public int increment(){
int v;
do {
v = value .get();
} while (v != value .compareAndSwap(v, v + 1));
return v + 1;
}
}
3 JVM对CAS的支持
java.util.concurrent.atomic 类的小工具包,支持在单个变量上解除锁的线程安全编程。 AtomicBoolean 能够用原子方式更新的 boolean 值。
AtomicInteger 能够用原子方式更新的 int 值。
AtomicIntegerArray 能够用原子方式更新其元素的 int 数组。
AtomicIntegerFieldUpdater<T> 基于反射的有用工具,能够对指定类的指定 volatile int 字段进行原子更新。
AtomicLong 能够用原子方式更新的 long 值。 AtomicLongArray 能够用原子方式更新其元素的 long 数组。 AtomicLongFieldUpdater<T> 基于反射的有用工具。能够对指定类的指定 volatile long 字段进行原子更新。
AtomicMarkableReference<V> AtomicMarkableReference 维护带有标记位的对象引用。能够原子方式对其进行更新。
AtomicReference<V> 能够用原子方式更新的对象引用。 AtomicReferenceArray<E> 能够用原子方式更新其元素的对象引用数组。
AtomicReferenceFieldUpdater<T,V> 基于反射的有用工具。能够对指定类的指定 volatile 字段进行原子更新。
AtomicStampedReference<V> AtomicStampedReference 维护带有整数“标志”的对象引用,能够用原子方式对其进行更新。
三、原子变量类
1 原子变量是一种“更好的volatile”
import java.util.concurrent.atomic.AtomicReference;
public class CasNumberRange {
private static class IntPair{
final int lower ; // 不变性条件: lower <= upper
final int upper ;
public IntPair( int lower, int upper) {
this .lower = lower;
this .upper = upper;
}
}
private final AtomicReference<IntPair> values =
new AtomicReference<IntPair>( new IntPair(0, 0));
public int getLower(){
return values .get(). lower;
}
public int getUpper(){
return values .get(). upper;
}
public void setLower( int i){
while (true ){
IntPair oldv = values .get();
if (i > oldv.upper ){
throw new IllegalArgumentException( "Cant't set lower to " + i + " > upper");
}
IntPair newv = new IntPair(i, oldv.upper );
if (values .compareAndSet(oldv, newv)){
return ;
}
}
}
// 对setUpper採用相似的方法
}
2 性能比較:锁与原子变量
使用ReentrantLock、AtomicInteger、ThreadLocal比較,通常情况下效率排序是ThreadLocal > AtomicInteger > ReentrantLock。
四、非堵塞算法
1 非堵塞的栈
import java.util.concurrent.atomic.AtomicReference;
public class ConcurrentStack<E> {
private AtomicReference<Node<E>> top = new AtomicReference<ConcurrentStack.Node<E>>();
public void push(E item){
Node<E> newHead = new Node<E>(item);
Node<E> oldHead;
do {
oldHead = top .get();
newHead. next = oldHead;
} while (!top .compareAndSet(oldHead, newHead));
}
public E pop(){
Node<E> oldHead;
Node<E> newHead;
do {
oldHead = top .get();
if (oldHead == null) {
return null ;
}
newHead = oldHead. next ;
} while (!top .compareAndSet(oldHead, newHead));
return oldHead.item ;
}
private static class Node<E>{
public final E item;
public Node<E> next ;
public Node(E item){
this .item = item;
}
}
}
2 非堵塞的链表
import java.util.concurrent.atomic.AtomicReference; @ ThreadSafe
public class LinkedQueue<E> {
private static class Node<E>{
final E item;
final AtomicReference<Node<E>> next; public Node(E item, Node<E> next){
this .item = item;
this .next = new AtomicReference<Node<E>>(next);
}
} private final Node<E> dummy = new Node<E>( null , null );
private final AtomicReference<Node<E>> head =
new AtomicReference<Node<E>>(dummy);
private final AtomicReference<Node<E>> tail =
new AtomicReference<Node<E>>(dummy); public boolean put(E item){
Node<E> newNode = new Node<E>(item, null);
while (true ){
Node<E> curTail = tail.get();
Node<E> tailNext = curTail.next.get();
if (curTail == tail.get()){
if (tailNext != null){
// 队列处于中间状态,推进尾节点
tail.compareAndSet(curTail, tailNext);
} else {
// 处于稳定状态, 尝试插入新节点
if (curTail.next.compareAndSet( null, newNode)){
// 插入操作成功,尝试推进尾节点
tail.compareAndSet(curTail, tailNext);
return true ;
}
}
}
}
}
}
3 原子的域更新器
private static class Node<E>{
private final E item;
private volatile Node<E> next;
public Node(E item){
this.item = item;
}
}
private static AtomicReferenceFieldUpdater<Node, Node> nextUpdater
= AtomicReferenceFieldUpdater.newUpdater(Node.class , Node.class , "next" );
4 ABA问题
《Java并发编程实战》第十五章 原子变量与非堵塞同步机制 读书笔记的更多相关文章
- java并发编程实战:第十五章----原子变量与非阻塞机制
非阻塞算法:使用底层的原子机器指令(例如比较并交换指令)代替锁来确保数据在并发访问中的一致性 应用于在操作系统和JVM中实现线程 / 进程调度机制.垃圾回收机制以及锁和其他并发数据结构 可伸缩性和活跃 ...
- 《Java并发编程实战》第五章 同步容器类 读书笔记
一.同步容器类 1. 同步容器类的问题 线程容器类都是线程安全的.可是当在其上进行符合操作则须要而外加锁保护其安全性. 常见符合操作包括: . 迭代 . 跳转(依据指定顺序找到当前元素的下一个元素) ...
- Java并发编程实战 第15章 原子变量和非阻塞同步机制
非阻塞的同步机制 简单的说,那就是又要实现同步,又不使用锁. 与基于锁的方案相比,非阻塞算法的实现要麻烦的多,但是它的可伸缩性和活跃性上拥有巨大的优势. 实现非阻塞算法的常见方法就是使用volatil ...
- java并发编程实战:第五章----基础构建模块
委托是创建线程安全类的一个最有效的策略:只需让现有的线程安全类管理所有的状态即可. 一.同步容器类 1.同步容器类的问题 同步容器类都是线程安全的,容器本身内置的复合操作能够保证原子性,但是当在其上进 ...
- 【java并发编程实战】第五章:基础构建模块
1.同步容器类 它们是线程安全的 1.1 vector和hashtable. 和Collections.synchronizeXxx()一样.实现方式就是在每个方法里面加入synchronize代码块 ...
- 《Java并发编程实战》第三章 对象的共享 读书笔记
一.可见性 什么是可见性? Java线程安全须要防止某个线程正在使用对象状态而还有一个线程在同一时候改动该状态,并且须要确保当一个线程改动了对象的状态后,其它线程能够看到发生的状态变化. 后者就是可见 ...
- 《Java并发编程实战》第十一章 性能与可伸缩性 读书笔记
造成开销的操作包含: 1. 线程之间的协调(比如:锁.触发信号以及内存同步等) 2. 添加�的上下文切换 3. 线程的创建和销毁 4. 线程的调度 一.对性能的思考 1 性能与可伸缩性 执行速度涉及下 ...
- 《Java并发编程实战》第六章 任务运行 读书笔记
一. 在线程中运行任务 无限制创建线程的不足 .线程生命周期的开销很高 .资源消耗 .稳定性 二.Executor框架 Executor基于生产者-消费者模式.提交任务的操作相当于生产者.运行任务的线 ...
- 《Java并发编程实战》第七章 取消与关闭 读书笔记
Java没有提供不论什么机制来安全地(抢占式方法)终止线程,尽管Thread.stop和suspend等方法提供了这种机制,可是因为存在着一些严重的缺陷,因此应该避免使用. 但它提供了中断In ...
随机推荐
- linux中fork()函数详解(转)
转自:http://blog.csdn.net/jason314/article/details/5640969 一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过 ...
- VBSCRIPT事件绑定(隐式)
很多新版的浏览器都开始不支持VBSCRIPT 所以系统开始不断地有script错误,开始比较多地接触VBSCRIPT vbscript 和javascript 事件绑定的类似方法为 vbscript: ...
- [Mac][phpMyAdmin] [2002] No such file or directory
我从phpMyAdmin的官网下载了最新版,将它解压到 /Library/WebServer/Documents 下,然后把文件夹改名 phpmyadmin . 接着输入在浏览器中输入 localho ...
- (3)java棧
java棧和函数调用的关系图 [名词解释]--->java棧是一块线程的私有空间--->java的棧是先进后出的数据结构.函数返回,则该函数的棧帧被弹出.--->一个函数对应一个棧帧 ...
- InstallShield高级应用--检查是否安装ORACLE或SQL Server
InstallShield高级应用--检查是否安装ORACLE或SQL Server 实现原理:判断是否存在,是通过查找注册表是否含有相应标识来判断的. 注意:XP与WIN7系统注册表保存方式不一 ...
- MFC中状态栏显示鼠标坐标位置
原文:MFC中状态栏显示鼠标坐标位置,蝈蝈 1,利用MFC向导创建一个应用工程ewq. 2,打开ResourceView,右击Menu菜单,插入Menu,在空白处双击,Caption中填入Point. ...
- NGINX(四)配置解析
前言 nginx配置解析是在初始化ngx_cycle_t数据结构时,首先解析core模块,然后core模块依次解析自己的子模块. 配置解析过程 nginx调用ngx_conf_parse函数进行配置文 ...
- NGINX(六)扩展
前言 nginx模块化设计, 添加扩展模块变得容易, 下面开发一个非常简单的扩展模块, 实现返回http请求的头部内容, 配置标记是ping_pong, 配置在NGX_HTTP_LOC_CONF中. ...
- WebDriver运行异常列表
1. WebDriverException: Component returned failure code: 0x804b000a 这个异常通常是因为在navigate到url时,丢失了http,务 ...
- 初识Ajax---简单的Ajax应用实例
原文: http://www.ido321.com/347.html 从网页前端输入提示范围内的字符,然后显示从后台返回的结果 1: <html> 2: <head> 3: & ...