• 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());
        }
    }
  • 相关阅读:
    Jenkins 主备master-slave模式搭建
    vbox 相关
    jenkins 常见问题汇总
    linux git patch 和patch以及git diff 命令
    Linux中的free命令
    MySQL Show命令的使用
    MySQL 加锁处理分析 转
    共享锁【S锁】 排他锁【X锁】
    MySQL分库分表环境下全局ID生成方案 转
    mysql性能的检查和调优方法
  • 原文地址:https://www.cnblogs.com/yaowen/p/10746137.html
Copyright © 2020-2023  润新知