在CAS操做中,會出現ABA問題。java
public final int incrementAndGet() { for (;;) { int current = get(); int next = current + 1; if (compareAndSet(current, next)) return next; } } //這個方法的作法爲先獲取到當前的 value 屬性值, // 而後將 value 加 1,賦值給一個局部的 next 變量, // 然而,這兩步都是非線程安全的, //可是內部有一個死循環,不斷去作compareAndSet操做,直到成功爲止, // 也就是修改的根本在compareAndSet方法裏面,compareAndSet()方法的