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());
        }
    }
  • 相关阅读:
    MySQL-索引
    MySQL-存储引擎
    MySQL-基本概念
    Elasticsearch-分片原理2
    Elasticsearch-分片原理1
    [NOIP模拟33]反思+题解
    [NOIP模拟测试32]反思+题解
    [NOIP模拟测试31]题解
    [jzoj5840]Miner 题解(欧拉路)
    [NOIP模拟测试30]题解
  • 原文地址:https://www.cnblogs.com/yaowen/p/10746137.html
Copyright © 2011-2022 走看看