JUC知识点框架

BxdBlog / 2023-06-04 / 原文

JUC(java.util.concurrent)

  • atomic

    • AtomicBoolean
    • AtomicInteger
    • AtomicLong
    • AtomicReference
  • locks

    • AQS(AbstractQueuedSynchronizer)
    • ReentrantLock
    • Condition
    • LockSupport
    • ReentrantReadWriteLock
    • StampedLock
  • AQS典型应用

    • ReentrantLock
    • CountDownLatch
    • Semaphore
    • CyclicBarrier
  • 容器

    • ConcurrentHashMap
    • CopyOnWriteArrayList
    • BlockingQueue(LinkedBlockingQueue,DelayQueue)
    • BlockingDeque(LinkedBlockingDeque)
  • 线程池

    • Executors
  • 其他

    • CompletableFuture
    • ForkJoinPool
    • FutureTask Callable
public final class Unsafe {
    private static final Unsafe theUnsafe;
	private Unsafe() {
    }
	@CallerSensitive
    public static Unsafe getUnsafe() {
        Class var0 = Reflection.getCallerClass();
        if (!VM.isSystemDomainLoader(var0.getClassLoader())) {
            throw new SecurityException("Unsafe");
        } else {
            return theUnsafe;
        }
    }
	public final native boolean compareAndSwapInt(Object var1, long var2, int var4, int var5);
	public final int getAndSetInt(Object var1, long var2, int var4) {
        int var5;
        do {
            var5 = this.getIntVolatile(var1, var2);
        } while(!this.compareAndSwapInt(var1, var2, var5, var4));

        return var5;
    }
    public final int getAndAddInt(Object var1, long var2, int var4) {
        int var5;
        do {
            var5 = this.getIntVolatile(var1, var2);
        } while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4));

        return var5;
    }
}



public class AtomicInteger extends Number implements java.io.Serializable {

    private static final long serialVersionUID = 6214790243416807050L;

    private static final Unsafe unsafe = Unsafe.getUnsafe();
	
    private static final long valueOffset;

    static {
        try {
            valueOffset = unsafe.objectFieldOffset
                (AtomicInteger.class.getDeclaredField("value"));
        } catch (Exception ex) { throw new Error(ex); }
    }

    private volatile int value;


    public AtomicInteger(int initialValue) {
        value = initialValue;
    }

    public AtomicInteger() {
    }


    public final int get() {
        return value;
    }

    public final void set(int newValue) {
        value = newValue;
    }
	/**
     * Atomically sets to the given value and returns the old value.
     *
     * @param newValue the new value
     * @return the previous value
     */
    public final int getAndSet(int newValue) {
        return unsafe.getAndSetInt(this, valueOffset, newValue);
    }
    /**
     * Atomically sets the value to the given updated value
     * if the current value {@code ==} the expected value.
     *
     * @param expect the expected value
     * @param update the new value
     * @return {@code true} if successful. False return indicates that
     * the actual value was not equal to the expected value.
     */
    public final boolean compareAndSet(int expect, int update) {
        return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
    }
    /**
     * Atomically adds the given value to the current value.
     *
     * @param delta the value to add
     * @return the previous value
     */
    public final int getAndAdd(int delta) {
        return unsafe.getAndAddInt(this, valueOffset, delta);
    }
	    /**
     * Atomically adds the given value to the current value.
     *
     * @param delta the value to add
     * @return the updated value
     */
    public final int addAndGet(int delta) {
        return unsafe.getAndAddInt(this, valueOffset, delta) + delta;
    }

}