zoukankan      html  css  js  c++  java
  • 通过 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;
      }
    
    }
  • 相关阅读:
    python 查看所有的关键字
    使用yum命令报错File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: SyntaxError: invalid syntax问题
    安装Python3.6.2报错:zipimport.ZipImportError: can't decompress data; zlib not available
    在CentOS-7.0中安装Python3.6.2
    批处理基础
    linux创建线程之pthread_create
    嵌入式 printf函数
    滤波算法
    单片机启动文件
    SUID、SGID详解
  • 原文地址:https://www.cnblogs.com/frankyou/p/12048439.html
Copyright © 2011-2022 走看看