Atomic Access

In programming, an atomic action is one that effectively happens all at once. An atomic action cannot stop in the middle: it either happens completely, or it doesn't happen at all. No side effects of an atomic action are visible until the action is complete.

We have already seen that an increment expression, such as c++, does not describe an atomic action. Even very simple expressions can define complex actions that can decompose into other actions. However, there are actions you can specify that are atomic:

  • Reads and writes are atomic for reference variables and for most primitive variables (all types except long anddouble).
  • Reads and writes are atomic for all variables declared volatile (including long and double variables).

Atomic actions cannot be interleaved, so they can be used without fear of thread interference. However, this does not eliminate all need to synchronize atomic actions, because memory consistency errors are still possible. Using volatilevariables reduces the risk of memory consistency errors, because any write to a volatile variable establishes a happens-before relationship with subsequent reads of that same variable. This means that changes to a volatile variable are always visible to other threads. What's more, it also means that when a thread reads a volatile variable, it sees not just the latest change to the volatile, but also the side effects of the code that led up the change.

Using simple atomic variable access is more efficient than accessing these variables through synchronized code, but requires more care by the programmer to avoid memory consistency errors. Whether the extra effort is worthwhile depends on the size and complexity of the application.

Some of the classes in the java.util.concurrent package provide atomic methods that do not rely on synchronization. We'll discuss them in the section on High Level Concurrency Objects.

 
译文:
原子操作
 在编程中,原子操作指动作只有效的发生一次。原子操作不能在中途停止。它要么完全发生,要么不发生。直到这个原子性早错完成之后才是可见的。
我们已经看到了一种自增运算,例如C++,不是原子性操作,即使是很简单的表达式,也可以分解成复杂的操作。但是,这里提供了指定操作为原子性操作的方法。
  • 读取和写入原子性的引用变量和最基础的变量类型(除了long和double).
  • 读取和写入原子性的带有volatile声明的变量 (包括 long 和double 变量).

原子性操作不会出现交叉,因此不会出现线程交叉性冲突。然而,这并不能消除原子性操作需要用synchroinze方法限制,因为内存一致性错误仍然还是存在的。使用volatile变量降低了内存一致性错误,因为volatile变量在写入的时候就确定了对于后续操作这个变量的事先发生的关系。这以为对于volatile变量的改变,对于其他线程始终是可见的。更重要的是,当一个线程读取volatile变量的时候,它不仅看到了这个变量最新的改变,而且能够在它的代码中改变这个变量的值。

利用简单的原子性操作比用synchroinze方法更加的有效率,但是,要求程序员更加的注意内存一致性错误。是否更多的工作需要做,依赖于应用程序的大小和复杂性。

许多在java.util.concurrent包中的类提供了不依赖于synchroinze的原子性方法。我们会在 High Level Concurrency Objects这一节讲述这些方法。

【翻译十一】java-原子性操作的更多相关文章

  1. 并发库应用之二 & Java原子性操作类应用

    Java5的线程并发库中,提供了一组atomic class来帮助我们简化同步处理.基本工作原理是使用了同步synchronized的方法实现了对一个long, integer, 对象的增.减.赋值( ...

  2. 一段JAVA代码了解多线程,JUC、CAS原子性操作。

    @Test public void testPaceController_multiThread() throws InterruptedException { final PaceControlle ...

  3. Java中volatile如何保证long和double的原子性操作

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11426473.html 关键字volatile的主要作用是使变量在多个线程间可见,但无法保证原子性,对 ...

  4. 02.java并发编程之原子性操作

    一.原子性操作 1.ThreadLocal 不同线程操作同一个 ThreadLocal 对象执行各种操作而不会影响其他线程里的值 注意:虽然ThreadLocal很有用,但是它作为一种线程级别的全局变 ...

  5. Java并发基础10:原子性操作类的使用

    在 java5 以后,我们接触到了线程原子性操作,也就是在修改时我们只需要保证它的那个瞬间是安全的即可,经过相应的包装后可以再处理对象的并发修改,本文总结一下Atomic系列的类的使用方法,其中包含: ...

  6. 十一天 python操作rabbitmq、redis

    1.启动rabbimq.mysql 在""运行""里输入services.msc,找到rabbimq.mysql启动即可 2.启动redis 管理员进入cmd, ...

  7. 关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10

    关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10 转载请标明出处,否则死全家.选择[复制链接]即可得到出处. (* ...

  8. 多线程(四)~数据操作的原子性,使用原子性操作AutomicInteger替换非原子性的i++的操作

    这一章,我们要来验证volatile关键字不是原子性的,OK,还是用代码来说话. ①.线程类,操作i++ 500次 package com.multiThread.thread; publicclas ...

  9. 新姿势!Redis中调用Lua脚本以实现原子性操作

    背景:有一服务提供者Leader,有多个消息订阅者Workers.Leader是一个排队程序,维护了一个用户队列,当某个资源空闲下来并被分配至队列中的用户时,Leader会向订阅者推送消息(消息带有唯 ...

随机推荐

  1. 35 网络相关函数(三)——live555源码阅读(四)网络

    35 网络相关函数(三)——live555源码阅读(四)网络 35 网络相关函数(三)——live555源码阅读(四)网络 简介 5)NoReuse不重用地址类 6)initializeWinsock ...

  2. ccs3中icon转换为字体的方法

    小图标转换为字体有几大优点 文件小,一般50k以内 避免了加载多个icons,减少了加载次数,有利于页面优化. 兼容性很好,可以随便放大缩小,都能正常显示. 维护起来也很简单,只用找到这个字体文件(比 ...

  3. 线段树基础模板&&扫描线

    线段树的单点更新+区间求和 hdu1166敌兵布阵 Input 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N<=),表示敌人有N个工兵营地 ,接下来有N个正整数,第i个正整 ...

  4. Java abstract

    abstract修饰符可以修饰类和方法. (1)abstract修饰类,会使这个类成为一个抽象类,这个类将不能生成对象实例,但可以做为对象变量声明的类型,也就是编译时类型.抽象类就相当于一类的半成品, ...

  5. struts2和hibernate整合的小Demo

    jar包下载地址 创建一个web项目. 导入jar包 配置web.xml <?xml version="1.0" encoding="UTF-8"?> ...

  6. Fibonacci 1

    Fibonacci 1 题面 \[F_0=0,F_1=1,F_n=F_{n-1}+F_{n-2}\] 给定\(n\),求 \[S(n)=\sum_{i=1}^{n}F_nF_{n-1}\] 数据格式 ...

  7. php中array_filter的使用

    这是今天在做作业的时候遇到的一个问题,不知道大家有没有遇到同样的问题,就是去除数组中不符合我们条件的数据并且保留其键名. 言归正传: array array_filter ( array $input ...

  8. http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/

    http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/ box-shadow  :   http://blog.csdn.net/freshlover/a ...

  9. JStorm集群的部署

    JStorm是一个类似Hadoop MapReduce的系统,不同的是JStorm是一套基于流水线的消息处理机制,是阿里基于Storm优化的版本,和Storm一样是一个分布式实时计算的系统,从开发角度 ...

  10. How to raise exceptions in Delphi

    uses SysUtils; procedure RaiseMyException; begin raise Exception.Create('Hallo World!'); end;