前几篇博客中我们已经介绍了线程、volatile、synchronized和cas自旋相关的知识,接下来我介绍一下jdk提供的并发编程包java.util.concurrent中相关的实现类知识。
AtomicInteger简单来说就是一个能进行原子操作的Integer,这样在多线程操作下对AtomicInteger的操作是原子操作的,操作后的值对所有线程都是立即可见的。简单来说其实现就是使用的volatile变量 value和sun.misc.Unsafe提供的CAS操作完成的,volatile变量value就表明任意一个线程对value的操作都是对其他线程可见的,并且对于value的修改使用Unsafe提供的方法实现线程不阻塞来完成对value值的修改等操作。
AtomicInteger的变量值:
- private volatile int value;
- public final int get() {
- return value;
- }
- public final void set(int newValue) {
- value = newValue;
- }
- public final int incrementAndGet() {
- return unsafe.getAndAddInt(this, valueOffset, 1) + 1;
- }
- public final int decrementAndGet() {
- return unsafe.getAndAddInt(this, valueOffset, -1) - 1;
- }
AtomicInteger源码:
- 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;
- }
- public final void lazySet(int newValue) {
- unsafe.putOrderedInt(this, valueOffset, newValue);
- }
- public final int getAndSet(int newValue) {
- return unsafe.getAndSetInt(this, valueOffset, newValue);
- }
- public final boolean compareAndSet(int expect, int update) {
- return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
- }
- public final boolean weakCompareAndSet(int expect, int update) {
- return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
- }
- public final int getAndIncrement() {
- return unsafe.getAndAddInt(this, valueOffset, 1);
- }
- public final int getAndDecrement() {
- return unsafe.getAndAddInt(this, valueOffset, -1);
- }
- public final int getAndAdd(int delta) {
- return unsafe.getAndAddInt(this, valueOffset, delta);
- }
- public final int incrementAndGet() {
- return unsafe.getAndAddInt(this, valueOffset, 1) + 1;
- }
- public final int decrementAndGet() {
- return unsafe.getAndAddInt(this, valueOffset, -1) - 1;
- }
- public final int addAndGet(int delta) {
- return unsafe.getAndAddInt(this, valueOffset, delta) + delta;
- }
- public final int getAndUpdate(IntUnaryOperator updateFunction) {
- int prev, next;
- do {
- prev = get();
- next = updateFunction.applyAsInt(prev);
- } while (!compareAndSet(prev, next));
- return prev;
- }
- public final int updateAndGet(IntUnaryOperator updateFunction) {
- int prev, next;
- do {
- prev = get();
- next = updateFunction.applyAsInt(prev);
- } while (!compareAndSet(prev, next));
- return next;
- }
- public final int getAndAccumulate(int x,
- IntBinaryOperator accumulatorFunction) {
- int prev, next;
- do {
- prev = get();
- next = accumulatorFunction.applyAsInt(prev, x);
- } while (!compareAndSet(prev, next));
- return prev;
- }
- public final int accumulateAndGet(int x,
- IntBinaryOperator accumulatorFunction) {
- int prev, next;
- do {
- prev = get();
- next = accumulatorFunction.applyAsInt(prev, x);
- } while (!compareAndSet(prev, next));
- return next;
- }
- public String toString() {
- return Integer.toString(get());
- }
- public int intValue() {
- return get();
- }
- public long longValue() {
- return (long)get();
- }
- public float floatValue() {
- return (float)get();
- }
- public double doubleValue() {
- return (double)get();
- }
- }