AtomicInteger的CAS算法浅析
之前浅析过自旋锁(自旋锁浅析),我们知道它的实现原理就是CAS算法。CAS(Compare 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)。如果内存位置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.
*
* <p><a href="package-summary.html#weakCompareAndSet">May fail
* spuriously and does not provide ordering guarantees</a>, 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个成员变量和两个方法。方法比较简单,一个是返回自增前的值,一个是返回自增后的值。
第一个成员变量是unsafe,这个必不可少,没了它CAS就是浮云。CAS算法用到了Unsafe对象的compareAndSwapInt方法,而它是一个本地方法,所以实现源码到此为止,没法再跟进去了。其实CAS算法的精华也就在于此,所以很遗憾。但至少我们知道Unsafe总共有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);
第二个成员变量是valueOffset,它是共享变量value在AtomicInteger对象上的内存偏移量。它作为compareAndSwapInt的第二个参数,用于修改共享变量value的值。
最后就是value了,上面已经介绍了,它就是例子中的count,是共享变量,也就是多线程并发中被追杀的共享资源。它使用volatile修饰,解决了可见性和有序性问题,再由unsafe的CAS保证了原子性,3大问题都解决了,多线程并发问题也就解决了。
回过头再看那标黄的两个方法,实现都是unsafe的getAndAddInt,点进去瞧瞧,发现它不是本地方法:
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;
}
代码看起来是不是有点眼熟?没错,还是自旋锁的套路,只不过这里用到的是Unsafe的CAS算法,而我们的自旋锁用到的是多套了一层马甲的AtmoicXXX的CAS算法,所以说到底,我们用的还是Unsafe的CAS。通过循环,先获取当前值var5(怎么获取当前值的?getIntVolatile就不用看了,还是本地方法),再计算更新值var5+var4,然后通过compareAndSwapInt方法设置value变量。如果compareAndSwapInt方法返回失败,表示value变量的值被别的线程更改了,所以需要循环获取value变量的最新值,再次通过compareAndSwapInt方法设置value变量,直至设置成功,跳出循环,返回更新前的值。
从上面看到,CAS底层实现依赖于Unsafe包,我们只要明白CAS的原理即可:预期值与当前值一致,那么执行更新,否则死循环尝试更新,直到成功。
AtomicInteger的CAS算法浅析的更多相关文章
- java并发:AtomicInteger 以及CAS无锁算法【转载】
1 AtomicInteger解析 众所周知,在多线程并发的情况下,对于成员变量,可能是线程不安全的: 一个很简单的例子,假设我存在两个线程,让一个整数自增1000次,那么最终的值应该是1000:但是 ...
- Compare and Swap [CAS] 算法
一个Java 5中最好的补充是对原子操作的支持类,如AtomicInteger,AtomicLong等.这些类帮助你减少复杂的(不必要的)多线程代码,实际上只是完成一些基本操作,如增加或减少多个线程之 ...
- 三、原子变量与CAS算法
原子变量:jdk1.5 后 java.util.concurrent.atomic 包下提供了常用的原子变量: - AtomicBoolean - AtomicInteger - AtomicLong ...
- Java多线程系列——原子类的实现(CAS算法)
1.什么是CAS? CAS:Compare and Swap,即比较再交换. jdk5增加了并发包java.util.concurrent.*,其下面的类使用CAS算法实现了区别于synchronou ...
- (一)juc线程高级特性——volatile / CAS算法 / ConcurrentHashMap
1. volatile 关键字与内存可见性 原文地址: https://www.cnblogs.com/zjfjava/category/979088.html 内存可见性(Memory Visibi ...
- 原子性 CAS算法
一. i++ 的原子性问题 1.问题的引入: i++ 的实际操作分为三个步骤:读--改--写 实现线程,代码如下: public class AtomicDemo implements Runnabl ...
- Java多线程-----原子变量和CAS算法
原子变量 原子变量保证了该变量的所有操作都是原子的,不会因为多线程的同时访问而导致脏数据的读取问题 Java给我们提供了以下几种原子类型: AtomicInteger和Ato ...
- Java-JUC(三):原子性变量与CAS算法
原子性 并发程序正确地执行,必须要保证原子性.可见性以及有序性.只要有一个没有被保证,就有可能会导致程序运行不正确. 原子性:一个操作或多个操作要么全部执行完成且执行过程不被中断,要么就不执行. 可见 ...
- 原子变量与CAS算法
原子变量 为了引出原子变量这个概念,我们先看一个例子. package com.ccfdod.juc; public class TestAtomicDemo { public static void ...
随机推荐
- Codeforces 750 E New Year and Old Subsequence
E. New Year and Old Subsequence 思路:线段树维护矩阵乘法. 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #p ...
- vue-cli 安装使用
全局安装vue-cli,使用命令npm install -g vue-cli. 下载模板代码,使用命令vue init webpack my-project,之后会有一些询问,按需填写即可. 最后会看 ...
- 小技巧:with用法 pycharm控制台输出带颜色的文字 打印进度条的
with用法 with用法在python中是一个很独特的用法,因为别的语言的中没有这个用法.所以针对这个特点我们来做一次总结,什么样的情况下可以同with 我们学到的有文件的操作,和acquire ...
- m_strcpy
自己实现strcpy函数 #include <stdio.h> #include <assert.h> //如果它的条件返回错误,则终止程序执行 char *m_strcpy( ...
- 0015SpringBoot结合thymeleaf实现点击菜单高亮显示
1.点击菜单,经过Controller层处理,正常定位到视图页面 2.编写抽象出公共片段的html,根据参数判断是否加高亮样式 3.多个目标页面引用步骤2中抽象出的公共片段,传不同的参数 具体实现如下 ...
- TCP三次握手,四次回收的每一步的解释
三次握手 第一次握手:主机A发送位码为syn=1,随机产生seq number=10001的数据包到服务器,主机B由SYN=1知道,A要求建立联机,此时状态为SYN_SENT: 第二次握手:主机B收到 ...
- 理解*arg 、**kwargs
这两个是python中的可变参数.*args表示任何多个无名参数,它是一个tuple(元祖):**kwargs表示关键字参数,它是一个dict(字典).并且同时使用*args和**kwargs时,必须 ...
- 转: java 双向map
package tools; import java.util.HashMap; public class DuplexMap<K,V> { class Entry{ K k; V v; ...
- sql server 悲观锁和乐观锁的作用
sql server对并发的处理-乐观锁和悲观锁 假如两个线程同时修改数据库同一条记录,就会导致后一条记录覆盖前一条,从而引发一些问题. 例如: 一个售票系统有一个余票数,客户端每调用一次出票方法,余 ...
- luogu 2617
动态区间 $k$ 大主席树 + 树状数组树状数组的每个点对应一颗线段树首先将所有点加入数据结构 枚举 x code: for(int i = x; i <= n; i += Lowbit(i)) ...