Java 原子类 java.util.concurrent.atomic

1、i++为什么是非线程安全的

i++其实是分为3个步骤:获取i的值, 把i+1, 把i+1的结果赋给i

如果多线程执行i++操作,没有同步的话,结果可能不正确

如两个线程同时获取i的值,然后各自+1后,赋给i。正确的情况下i的值应该是加了2,但这里其实加了1而且,前面的结果被覆盖了。

通常做法:synchronized (synchronized方法,synchronized变量), 但这样做效率不是最优的。

2、AtomicInteger的实现

主要依靠:1、volatile 保证了变量的可见性,所有线程不缓存volatile变量,需要时都从内存读取,这样能保证所有数据拿到的值都是最新的。

2、compareAndSet(int expect, int update)判断当前值==expect?当前值=update:错误;

这里做了两步操作,判断跟赋值。但因为cpu提供这样指令的支持,所有能保证这个操作时原子的。

  1. public class AtomicIntegerextends Number
    implements java.io.Serializable {
  2. private staticfinal
    long serialVersionUID = 6214790243416807050L;
  3. // setup to use Unsafe.compareAndSwapInt for updates
  4. private staticfinal Unsafe unsafe = Unsafe.getUnsafe();
  5. private staticfinal
    long valueOffset;
  6. static {
  7. try {
  8. valueOffset = unsafe.objectFieldOffset
  9. (AtomicInteger.class.getDeclaredField("value"));
  10. } catch (Exception ex) {
    throw new Error(ex); }
  11. }
  12. private volatileint value;
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;

3、AtomicInteger 中的set(int newValue) lazySet(int newValue)

void set(int newValue)

设置为给定值。 直接修改原始值,也就是i=newValue操作。

void lazySet(int newValue) 最后设置为给定值。

延时设置变量值,这个等价于set()方法,但是由于字段是volatile类型的,因此次字段的修改会比普通字段(非volatile字段)有稍微的性能延时(尽管可以忽略),所以如果不是想立即读取设置的新值,允许在“后台”修改值,那么此方法就很有用。如果还是难以理解,这里就类似于启动一个后台线程如执行修改新值的任务,原线程就不等待修改结果立即返回(这种解释其实是不正确的,但是可以这么理解)。





4、AtomicInteger中compareAndSet(int expect, int update) weakCompareAndSet(int expect, int update)

boolean weakCompareAndSet(int expect, int update)

如果当前值 == 预期值,则以原子方式将该设置为给定的更新值。JSR规范中说:以原子方式读取和有条件地写入变量但不
创建任何 happen-before 排序,因此不提供与除weakCompareAndSet 目标外任何变量以前或后续读取或写入操作有关的任何保证。大意就是说调用weakCompareAndSet时并不能保证不存在happen-before的发生(也就是可能存在指令重排序导致此操作失败)。但是从Java源码来看,其实此方法并没有实现JSR规范的要求,最后效果和compareAndSet是等效的,都调用了unsafe.compareAndSwapInt()完成操作。

  1. /**
  2. * Atomically sets the value to the given updated value
  3. * if the current value {@code ==} the expected value.
  4. *
  5. * @param expect the expected value
  6. * @param update the new value
  7. * @return true if successful. False return indicates that
  8. * the actual value was not equal to the expected value.
  9. */
  10. public finalboolean compareAndSet(int expect,int
    update) {
  11. return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
  12. }
  13. /**
  14. * Atomically sets the value to the given updated value
  15. * if the current value {@code ==} the expected value.
  16. *
  17. * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
  18. * and does not provide ordering guarantees, so is only rarely an
  19. * appropriate alternative to {@code compareAndSet}.
  20. *
  21. * @param expect the expected value
  22. * @param update the new value
  23. * @return true if successful.
  24. */
  25. public finalboolean weakCompareAndSet(int expect,int
    update) {
  26. return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
  27. }
    /**
* 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 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>May <a href="package-summary.html#Spurious">fail spuriously</a>
* and does not provide ordering guarantees, so is only rarely an
* appropriate alternative to {@code compareAndSet}.
*
* @param expect the expected value
* @param update the new value
* @return true if successful.
*/
public final boolean weakCompareAndSet(int expect, int update) {
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}

Java 原子类 java.util.concurrent.atomic的更多相关文章

  1. 原子类java.util.concurrent.atomic.*原理分析

    原子类java.util.concurrent.atomic.*原理分析 在并发编程下,原子操作类的应用可以说是无处不在的.为解决线程安全的读写提供了很大的便利. 原子类保证原子的两个关键的点就是:可 ...

  2. Java并发—原子类,java.util.concurrent.atomic包(转载)

    原子类 Java从JDK 1.5开始提供了java.util.concurrent.atomic包(以下简称Atomic包),这个包中 的原子操作类提供了一种用法简单.性能高效.线程安全地更新一个变量 ...

  3. java.util.concurrent.atomic 类包详解

    java.util.concurrent包分成了三个部分,分别是java.util.concurrent.java.util.concurrent.atomic和java.util.concurren ...

  4. 并发之java.util.concurrent.atomic原子操作类包

    15.JDK1.8的Java.util.concurrent.atomic包小结 14.Java中Atomic包的原理和分析 13.java.util.concurrent.atomic原子操作类包 ...

  5. Java多线程:CAS与java.util.concurrent.atomic

    锁的几种概念 悲观锁 总是假设最坏的情况,每次获取数据都认为别人会修改,所以拿数据时会上锁,一直到释放锁不允许其他线程修改数据.Java中如synchronized和reentrantLock就是这种 ...

  6. java并发编程:线程安全管理类--原子包--java.util.concurrent.atomic

    java.util.concurrent.atomic 的描述 AtomicBoolean 可以用原子方式更新的 boolean 值. AtomicInteger 可以用原子方式更新的 int 值. ...

  7. java.util.concurrent.atomic 包详解

    Atomic包的作用: 方便程序员在多线程环境下,无锁的进行原子操作 Atomic包核心: Atomic包里的类基本都是使用Unsafe实现的包装类,核心操作是CAS原子操作 关于CAS compar ...

  8. 谈论高并发(十二)分析java.util.concurrent.atomic.AtomicStampedReference看看如何解决源代码CAS的ABA问题

    于谈论高并发(十一)几个自旋锁的实现(五岁以下儿童)中使用了java.util.concurrent.atomic.AtomicStampedReference原子变量指向工作队列的队尾,为何使用At ...

  9. Java:多线程,java.util.concurrent.atomic包之AtomicInteger/AtomicLong用法

    1. 背景 java.util.concurrent.atomic这个包是非常实用,解决了我们以前自己写一个同步方法来实现类似于自增长字段的问题. 在Java语言中,增量操作符(++)不是原子的,也就 ...

随机推荐

  1. 第2章 Innodb 存储引擎

    一.InnoDB 体系架构 1.1后台线程 master thread:刷新内存中的数据到磁盘 io thread:处理 IO 请求,AIO purge thread:清理undo 页的回收 page ...

  2. 关于shopee平台接口(php)对接示例

    2018年8月之后,shopee开始使用新接口,需要进行授权操作 1.授权 public function getAuth(){ /** * @param ShopApiShopee $model * ...

  3. 关于volatile关键字

    来源:衡阳网站优化 在java核心卷1中对volatile关键字是这么描述的: volatile关键字为实例域的同步访问提供了一种免锁机制.如果声明一个域为volatile,那么编译器和虚拟机就知道该 ...

  4. 线程池-进程池-io模型

    一.线程池与进程池 什么是池?简单的说就是一个容器,一个范围 在保证计算机硬件安全的情况下最大限度的充分利用计算机, 池其实是降低了程序的运行效率,但是保证了计算机硬件的安全,也是实现了一个并发的效果 ...

  5. rare alleles

    I.4 Where the rare alleles are found p是基因A的频率,N是个体数目(也就是基因型个数,所以基因个数是2n,所以全部个体的基因A的个数是2np),p方是PAA,np ...

  6. vue项目打包部署elementUI的字体图标丢失问题

    自己搭建的Vue项目,没有使用vue-cli,引入elementUI时,使用的是webpack直接打包工具,发现字体图标丢失你 记录一下解决办法: webpack module配置:(build目录下 ...

  7. IOS之Core Foundation框架和Cocoa Foundation框架的区别(转)

    Core Foundation框架 (CoreFoundation.framework) 是一组C语言接口,它们为iOS应用程序提供基本数据管理和服务功能.下面列举该框架支持进行管理的数据以及可提供的 ...

  8. CI_CD(jenkins)公司实战_未完成版

    环境准备 #三台服务器,不同的ip centos   镜像时:CentOS Linux release 7.6.1810 (Core) gitlab-ce 版本是:11.11.3 jenkins   ...

  9. sin之舞---蓝桥杯练习

    问题描述 最近FJ为他的奶牛们开设了数学分析课,FJ知道若要学好这门课,必须有一个好的三角函数基本功.所以他准备和奶牛们做一个“Sine之舞”的游戏,寓教于乐,提高奶牛们的计算能力. 不妨设 An=s ...

  10. C#面向对象---对象成员、方法加载、引用类库

    一.方法重载: 1.两个函数同名,就互相构成方法的重载关系 2.重载的函数,必须跟其他函数之间具有不同的参数类型或参数个数 二.字段与属性 类的字段: 类里面是可以直接定义变量的,这些变量就叫类的字段 ...