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. BZOJ 1355: [Baltic2009]Radio Transmission

    Description 一个字符串最短周期. Sol KMP. 最短周期就是 \(n-next[n]\) 证明: 当该字符串不存在周期的时候 \(next[n]=0\) 成立. 当存在周期的时候 \( ...

  2. ffmpeg解码视频流

    //初始化.注册编解码器 avcodec_init(); av_register_all(); avformat_network_init(); //选取测试文件 char* FileName = & ...

  3. chkconfig用法 LINUX

    chkconfig用法 有时候为了方便管理,我们常常喜欢在Linux中将之安装为服务,然后就可以使用服务来管理. 但是当我们运行安装服务的命令时候,假设服务名为myservice #chkconfig ...

  4. JAVA上百实例源码以及开源项目

    简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级.中级.高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情.执着,对IT的憧憬. ...

  5. Appium+Robotframework实现Android应用的自动化测试-2:Windows中启动Appium和模拟器

    一.启动Appium 安装好了之后,在桌面或者菜单中找到Appium,分别双击或点击打开Appium.exe,如果一切正常,接着会出现一个Appium启动后的界面窗口,如下图所示. 1.1 Andro ...

  6. wxpython 基本的控件 (按钮)

    使用按钮工作 在wxPython 中有很多不同类型的按钮.这一节,我们将讨论文本按钮.位图按钮.开关按钮(toggle buttons )和通用(generic )按钮. 如何生成一个按钮? 在第一部 ...

  7. javascript 数组去重

    2015年5月15日 20:17:05 星期五 原理: .......(说不清楚, 自己看代码吧, 很简单.....) //去重 var hash_already_input = {}; for (v ...

  8. buffer正确的拼接方式

    var chunks = []; var size = 0; res.on('data',function(chunk){ chunks.push(chunk); size+= chunk.lengt ...

  9. 调试WebService

    1.运行WebService的调用程序 2.浏览器中运行asmx,这一步是为了让w3wp.exe出现在下一步的列表中 3.“工具”或“调试”菜单-->附加到进程 (MS为什么把同一功能放在不同的 ...

  10. java基础学习05(面向对象基础01)

    面向对象基础01 1.理解面向对象的概念 2.掌握类与对象的概念3.掌握类的封装性4.掌握类构造方法的使用 实现的目标 1.类与对象的关系.定义.使用 2.对象的创建格式,可以创建多个对象3.对象的内 ...