zoukankan      html  css  js  c++  java
  • AtomicReference

    public class AtomicReference<V> implements java.io.Serializable {
        private static final long serialVersionUID = -1848883965231344442L;
    
        private static final Unsafe unsafe = Unsafe.getUnsafe();
        private static final long valueOffset;
    
        static {
            try {
                valueOffset = unsafe.objectFieldOffset(AtomicReference.class.getDeclaredField("value"));
            } catch (Exception ex) { throw new Error(ex); }
        }
    
        private volatile V value;
    
        public AtomicReference(V initialValue) {
            value = initialValue;
        }
    
        public AtomicReference() {
        }
    
        public final V get() {
            return value;
        }
    
        public final void set(V newValue) {
            value = newValue;
        }
    
        public final void lazySet(V newValue) {
            unsafe.putOrderedObject(this, valueOffset, newValue);
        }
    
        public final boolean compareAndSet(V expect, V update) {
            return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
        }
    
        public final boolean weakCompareAndSet(V expect, V update) {
            return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
        }
    
        @SuppressWarnings("unchecked")
        public final V getAndSet(V newValue) {
            return (V)unsafe.getAndSetObject(this, valueOffset, newValue);
        }
    
        public final V getAndUpdate(UnaryOperator<V> updateFunction) {
            V prev, next;
            do {
                prev = get();
                next = updateFunction.apply(prev);
            } while (!compareAndSet(prev, next));
            return prev;
        }
    
        public final V updateAndGet(UnaryOperator<V> updateFunction) {
            V prev, next;
            do {
                prev = get();
                next = updateFunction.apply(prev);
            } while (!compareAndSet(prev, next));
            return next;
        }
    
        public final V getAndAccumulate(V x,BinaryOperator<V> accumulatorFunction) {
            V prev, next;
            do {
                prev = get();
                next = accumulatorFunction.apply(prev, x);
            } while (!compareAndSet(prev, next));
            return prev;
        }
    
        public final V accumulateAndGet(V x,BinaryOperator<V> accumulatorFunction) {
            V prev, next;
            do {
                prev = get();
                next = accumulatorFunction.apply(prev, x);
            } while (!compareAndSet(prev, next));
            return next;
        }
    
        public String toString() {
            return String.valueOf(get());
        }
    }
  • 相关阅读:
    git分支管理之创建与合并分支
    git分支管理
    git远程仓库之从远程库克隆
    git远程仓库之添加远程库
    git远程仓库
    Git时光机穿梭之删除文件
    Git时光机穿梭之撤销修改
    Git时光机穿梭之管理修改
    Git时光机穿梭之工作区和暂存区
    Git时光机穿梭之版本回退
  • 原文地址:https://www.cnblogs.com/yaowen/p/10746137.html
Copyright © 2011-2022 走看看