通过 UNSAFE 来实现一个 Atomic 的 CAS 辅助类原创

Posted FrankYou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过 UNSAFE 来实现一个 Atomic 的 CAS 辅助类原创相关的知识,希望对你有一定的参考价值。

 

public abstract class AbstractUnSafeCas<T> {

  @SuppressWarnings("unused")
  private volatile Object value;

  private static final AtomicHelper ATOMIC_HELPER;

  private final CountDownLatch countDownLatch = new CountDownLatch(1);

  static {
    ATOMIC_HELPER = new AtomicHelper();
  }

  private static class AtomicHelper {

    private static final sun.misc.Unsafe UNSAFE;

    private static final long VALUE_OFFSET;

    static {
      sun.misc.Unsafe unsafe;
      try {
        unsafe = sun.misc.Unsafe.getUnsafe();
      } catch (SecurityException tryReflectionInstead) {
        unsafe = AccessController.doPrivileged(new PrivilegedAction<Unsafe>() {
          @Override
          @SneakyThrows
          public Unsafe run() {
            Class<sun.misc.Unsafe> unsafeClass = sun.misc.Unsafe.class;
            for (Field f : unsafeClass.getDeclaredFields()) {
              f.setAccessible(true);
              Object o = f.get(null);
              if (unsafeClass.isInstance(o)) {
                return unsafeClass.cast(o);
              }
            }
            throw new NoSuchFieldError("the Unsafe");
          }
        });
      }
      try {
        VALUE_OFFSET = unsafe.objectFieldOffset(AbstractUnSafeAtomicHelper.class.getDeclaredField("value"));
      } catch (Exception e) {
        throw Throwables.propagate(e);
      }
      UNSAFE = unsafe;
    }

    public boolean casValue(AbstractUnSafeAtomicHelper<?> obj, Object expect, Object update) {
      return UNSAFE.compareAndSwapObject(obj, VALUE_OFFSET, expect, update);
    }

  }

  public boolean set(T value) {
    Preconditions.checkArgument(null != value, "The value to be setted is null");
    if (ATOMIC_HELPER.casValue(this, null, value)) {
      countDownLatch.countDown();
      return true;
    }
    return false;
  }

  @SneakyThrows
  public T get(boolean blockUntilValueIsNotNull) {
    if (blockUntilValueIsNotNull) {
      countDownLatch.await();
    }
    @SuppressWarnings("unchecked")
    T asT = (T) value;
    return asT;
  }

  @SneakyThrows
  public T get(Long timeout, TimeUnit timeUnit) {
    countDownLatch.await(timeout, timeUnit);
    @SuppressWarnings("unchecked")
    T asT = (T) value;
    return asT;
  }

}

 

以上是关于通过 UNSAFE 来实现一个 Atomic 的 CAS 辅助类原创的主要内容,如果未能解决你的问题,请参考以下文章

Java并发:CAS Unsafe Atomic

八:并发编程之Atomic&Unsafe魔法类详解

原子操作--sync/atomic的用法

第四节:并发编程之Atomic&Unsafe魔法类

Java源码Atomic部分源码解析

Dougli-Atomic原子包