标签:c style class blog code java
@ 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;
}
}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 维护带有整数“标志”的对象引用,可以用原子方式对其进行更新。
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采用类似的方法
}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;
}
}
}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 ;
}
}
}
}
}
}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" );《Java并发编程实战》第十五章 原子变量与非阻塞同步机制 读书笔记,布布扣,bubuko.com
《Java并发编程实战》第十五章 原子变量与非阻塞同步机制 读书笔记
标签:c style class blog code java
原文地址:http://blog.csdn.net/androiddevelop/article/details/28383937