AtomicIntegerのCASアルゴリズムを簡単に分析します.

34017 ワード

スピンロック(スピンロック浅析)を以前に解析したが、その実現原理はCASアルゴリズムであることを知っている.CAS(Compre and Swap)は比較して交換します.有名な無錠アルゴリズムとして、楽観的なロックの実現方法の一つです.JDK同時パッケージの中にもCASの姿が点滅しているコードがたくさんあります.CASアルゴリズムの同時領域の重要性と普遍性を考慮して、AtomicIntegerという原子類を結合して分析してみましょう.分析する前に、スピンロックテストコードを借りて直接AtomicIntegerの自己増加テスト結果を見ると、スピンロックと比較できます.
    @Test
    public void testAtomicInteger()
    {
        // 10     AtomicInteger  
        AtomicInteger ai = new AtomicInteger();
        for (int i = 0; i < 10; i++)
        {
            new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    //   1  
                    for (int j = 0; j < 10000; j++)
                    {
                        count = ai.incrementAndGet();
                    }

                    //           1,10          0,     
                    latch.countDown();
                }
            }).start();
        }

        //      
        try
        {
            latch.await();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        TestCase.assertEquals(count, 100000);
    }
実行結果:
count :100000,   :10  .
スピンロックより簡単ですか?必要なのは、AtomicInteger自身がすでにCASアルゴリズムを実現しているため、人は自然に同時に増加することに用いられます.以前にも述べたように、CASの原理は簡単で、現在のメモリ値(V)と元の値(A)と更新が期待される値(B)の3つが含まれています.メモリ位置Vの値が予想された原値Aと一致すると、プロセッサは自動的にこの位置値を新しい値Bに更新して、trueに戻る.そうでなければ、プロセッサはfalseに戻ります.上記の例を挙げると、10スレッドがそれぞれ自己増加操作を行っています.countは共有変数であり、この10スレッドに1つ追加されます.もしスレッド1がcountを100に追加すると、101に更新しようとしていますが、スレッド2が一足先にcountを101に追加すると、スレッド1はどうなりますか?最新のcount値を取得してから自増量します.具体的にどうやって実現しますか?これから見ます.直接的な観点を実現するために、上記の例を実現する方法を変えてもいいです.
package com.wulinfeng.test.testpilling;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntegerTest {

    //     
    private static int count;

    // 10        10
    private static CountDownLatch latch = new CountDownLatch(10);

    public static void main(String[] args) {
        // 10     AtomicInteger  
        AtomicInteger ai = new AtomicInteger();
        for (int i = 0; i < 10; i++) {
            final int threadNum = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //   1  
                    for (int j = 0; j < 10000; j++) {
                        count = ai.incrementAndGet();
                        System.out.println("  " + threadNum + ": " + count);
                    }

                    //           1,10          0,     
                    latch.countDown();
                }
            }).start();
        }

        //      
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
運転結果の後の行を切り取ります.
  1: 99993
  1: 99994
  1: 99995
  1: 99996
  1: 99997
  1: 99998
  1: 99999
  1: 100000
この後のログはスレッド1が追加されています.コンソールログを上に引っ張ると他のスレッドもずっと追加されています.この10スレッドはどのようにCASの呪法保護の下で互いに衝突していないのですか?AtomicIntegerのソースコードを見れば分かります.
package java.util.concurrent.atomic;
import java.util.function.IntUnaryOperator;
import java.util.function.IntBinaryOperator;
import sun.misc.Unsafe;

/**
 * An {@code int} value that may be updated atomically.  See the
 * {@link java.util.concurrent.atomic} package specification for
 * description of the properties of atomic variables. An
 * {@code AtomicInteger} is used in applications such as atomically
 * incremented counters, and cannot be used as a replacement for an
 * {@link java.lang.Integer}. However, this class does extend
 * {@code Number} to allow uniform access by tools and utilities that
 * deal with numerically-based classes.
 *
 * @since 1.5
 * @author Doug Lea
*/
public class AtomicInteger extends Number implements java.io.Serializable {
    private static final long serialVersionUID = 6214790243416807050L;

    // setup to use Unsafe.compareAndSwapInt for updates
    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;

    /**
     * Creates a new AtomicInteger with the given initial value.
     *
     * @param initialValue the initial value
     */
    public AtomicInteger(int initialValue) {
        value = initialValue;
    }

    /**
     * Creates a new AtomicInteger with initial value {@code 0}.
     */
    public AtomicInteger() {
    }

    /**
     * Gets the current value.
     *
     * @return the current value
     */
    public final int get() {
        return value;
    }

    /**
     * Sets to the given value.
     *
     * @param newValue the new value
     */
    public final void set(int newValue) {
        value = newValue;
    }

    /**
     * Eventually sets to the given value.
     *
     * @param newValue the new value
     * @since 1.6
     */
    public final void lazySet(int newValue) {
        unsafe.putOrderedInt(this, valueOffset, 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 sets the value to the given updated value
     * if the current value {@code ==} the expected value.
     *
     * 

May fail *spuriously and does not provide ordeng garantes, so is * only rarely an appropriate alternative to {

@code compareAndSet}. * * @param expect the expected value * @param update the new value * @return {@code true} if successful */ public final boolean weakCompareAndSet(int expect, int update) { return unsafe.compareAndSwapInt(this, valueOffset, expect, update); } /** * Atomically increments by one the current value. * * @return the previous value */ public final int getAndIncrement() { return unsafe.getAndAddInt(this, valueOffset, 1); } /** * Atomically decrements by one the current value. * * @return the previous value */ public final int getAndDecrement() { return unsafe.getAndAddInt(this, valueOffset, -1); } /** * 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 increments by one the current value. * * @return the updated value */ public final int incrementAndGet() { return unsafe.getAndAddInt(this, valueOffset, 1) + 1; } /** * Atomically decrements by one the current value. * * @return the updated value */ public final int decrementAndGet() { return unsafe.getAndAddInt(this, valueOffset, -1) - 1; } /** * 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; } /** * Atomically updates the current value with the results of * applying the given function, returning the previous value. The * function should be side-effect-free, since it may be re-applied * when attempted updates fail due to contention among threads. * * @param updateFunction a side-effect-free function * @return the previous value * @since 1.8 */ public final int getAndUpdate(IntUnaryOperator updateFunction) { int prev, next; do { prev = get(); next = updateFunction.applyAsInt(prev); } while (!compareAndSet(prev, next)); return prev; } /** * Atomically updates the current value with the results of * applying the given function, returning the updated value. The * function should be side-effect-free, since it may be re-applied * when attempted updates fail due to contention among threads. * * @param updateFunction a side-effect-free function * @return the updated value * @since 1.8 */ public final int updateAndGet(IntUnaryOperator updateFunction) { int prev, next; do { prev = get(); next = updateFunction.applyAsInt(prev); } while (!compareAndSet(prev, next)); return next; } /** * Atomically updates the current value with the results of * applying the given function to the current and given values, * returning the previous value. The function should be * side-effect-free, since it may be re-applied when attempted * updates fail due to contention among threads. The function * is applied with the current value as its first argument, * and the given update as the second argument. * * @param x the update value * @param accumulatorFunction a side-effect-free function of two arguments * @return the previous value * @since 1.8 */ 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; } /** * Atomically updates the current value with the results of * applying the given function to the current and given values, * returning the updated value. The function should be * side-effect-free, since it may be re-applied when attempted * updates fail due to contention among threads. The function * is applied with the current value as its first argument, * and the given update as the second argument. * * @param x the update value * @param accumulatorFunction a side-effect-free function of two arguments * @return the updated value * @since 1.8 */ 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; } /** * Returns the String representation of the current value. * @return the String representation of the current value */ public String toString() { return Integer.toString(get()); } /** * Returns the value of this {@code AtomicInteger} as an {@code int}. */ public int intValue() { return get(); } /** * Returns the value of this {@code AtomicInteger} as a {@code long} * after a widening primitive conversion. * @jls 5.1.2 Widening Primitive Conversions */ public long longValue() { return (long)get(); } /** * Returns the value of this {@code AtomicInteger} as a {@code float} * after a widening primitive conversion. * @jls 5.1.2 Widening Primitive Conversions */ public float floatValue() { return (float)get(); } /** * Returns the value of this {@code AtomicInteger} as a {@code double} * after a widening primitive conversion. * @jls 5.1.2 Widening Primitive Conversions */ public double doubleValue() { return (double)get(); } }
3つのメンバー変数と2つの方法が黄色で表示されます.方法は簡単で、一つは自己増加前の値を返し、一つは自己増加後の値を返します.
最初のメンバー変数はunsafeです.これはなくてはいけません.CASは浮雲です.CASアルゴリズムはUnisafeの対象のcompreAndSwapIntの方法を使用していますが、それは地元の方法ですので、ソースの実現はここまでです.実はCASアルゴリズムの精華もここにあるので、残念です.でも、少なくともUnisafeには全部で3つのCAS方法があると知っています.
    public final native boolean compareAndSwapObject(Object var1, long var2, Object var4, Object var5);

    public final native boolean compareAndSwapInt(Object var1, long var2, int var4, int var5);

    public final native boolean compareAndSwapLong(Object var1, long var2, long var4, long var6);
第二のメンバー変数は、AtomicIntegerオブジェクト上の共有変数valueのメモリオフセットです.compreAndSwapIntの2番目のパラメータとして、共有変数valueの値を変更します.
最後にvalueです.上で紹介しました.これは例のcountです.共有変数です.つまり、マルチスレッド同時で追殺された共有リソースです.それはvolatileを使って修飾して、視認性と秩序性の問題を解決して、unsafeのCASから原子性を保証して、3大問題はすべて解決して、マルチスレッドの合併の問題も解決しました.
振り返ってみますと、黄色の二つの方法が実現されているのは全部unsafeのget AndAddIntです.
    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;
    }
コードは見覚えがありますか?そうです.やはりスピンロックのロードです.ここで使ったのはUnisafeのCASアルゴリズムです.私たちのスピンロックはもう一枚のベストをセットしたAtmoicXXXのCASアルゴリズムです.ですから、やはりUnisafeのCASを使います.ループを通して、現在の値var 5を取得し(現在の値をどうやって取得しますか?get Int Volatileは見なくてもいいですか?それともローカル方法ですか?)、更新値var 5+var 4を計算して、その後、compreAndSwapInt方法でvalue変数を設定します.compreAndSwapIntメソッドが失敗したら、value変数の値が他のスレッドに変更されたことを示すため、value変数の最新値を循環的に取得し、再度compreAndSwapIntメソッドを通じてvalue変数を設定し、設定が成功するまでループを超えて更新前の値に戻ります.
上から見て、CASの底の実現はUnisafeの包みに依存して、私達はCASの原理を理解するのでさえすれば良いです:予想値は現在の値と一致して、それでは更新を実行して、さもなくばデッドサイクルは更新を試みて、成功に至ります.
  
転載先:https://www.cnblogs.com/wuxun1997/p/10974472.html