【翻译十一】java-原子性操作
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
longanddouble). - Reads and writes are atomic for all variables declared
volatile(includinglonganddoublevariables).
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.
- 读取和写入原子性的引用变量和最基础的变量类型(除了long和double).
- 读取和写入原子性的带有
volatile声明的变量(包括long和double变量).
原子性操作不会出现交叉,因此不会出现线程交叉性冲突。然而,这并不能消除原子性操作需要用synchroinze方法限制,因为内存一致性错误仍然还是存在的。使用volatile变量降低了内存一致性错误,因为volatile变量在写入的时候就确定了对于后续操作这个变量的事先发生的关系。这以为对于volatile变量的改变,对于其他线程始终是可见的。更重要的是,当一个线程读取volatile变量的时候,它不仅看到了这个变量最新的改变,而且能够在它的代码中改变这个变量的值。
利用简单的原子性操作比用synchroinze方法更加的有效率,但是,要求程序员更加的注意内存一致性错误。是否更多的工作需要做,依赖于应用程序的大小和复杂性。
许多在java.util.concurrent包中的类提供了不依赖于synchroinze的原子性方法。我们会在 High Level Concurrency Objects这一节讲述这些方法。
【翻译十一】java-原子性操作的更多相关文章
- 并发库应用之二 & Java原子性操作类应用
Java5的线程并发库中,提供了一组atomic class来帮助我们简化同步处理.基本工作原理是使用了同步synchronized的方法实现了对一个long, integer, 对象的增.减.赋值( ...
- 一段JAVA代码了解多线程,JUC、CAS原子性操作。
@Test public void testPaceController_multiThread() throws InterruptedException { final PaceControlle ...
- Java中volatile如何保证long和double的原子性操作
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11426473.html 关键字volatile的主要作用是使变量在多个线程间可见,但无法保证原子性,对 ...
- 02.java并发编程之原子性操作
一.原子性操作 1.ThreadLocal 不同线程操作同一个 ThreadLocal 对象执行各种操作而不会影响其他线程里的值 注意:虽然ThreadLocal很有用,但是它作为一种线程级别的全局变 ...
- Java并发基础10:原子性操作类的使用
在 java5 以后,我们接触到了线程原子性操作,也就是在修改时我们只需要保证它的那个瞬间是安全的即可,经过相应的包装后可以再处理对象的并发修改,本文总结一下Atomic系列的类的使用方法,其中包含: ...
- 十一天 python操作rabbitmq、redis
1.启动rabbimq.mysql 在""运行""里输入services.msc,找到rabbimq.mysql启动即可 2.启动redis 管理员进入cmd, ...
- 关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10
关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10 转载请标明出处,否则死全家.选择[复制链接]即可得到出处. (* ...
- 多线程(四)~数据操作的原子性,使用原子性操作AutomicInteger替换非原子性的i++的操作
这一章,我们要来验证volatile关键字不是原子性的,OK,还是用代码来说话. ①.线程类,操作i++ 500次 package com.multiThread.thread; publicclas ...
- 新姿势!Redis中调用Lua脚本以实现原子性操作
背景:有一服务提供者Leader,有多个消息订阅者Workers.Leader是一个排队程序,维护了一个用户队列,当某个资源空闲下来并被分配至队列中的用户时,Leader会向订阅者推送消息(消息带有唯 ...
随机推荐
- Triangle
动态规划 int minimumTotal (vector<vector<int>>& triangle) { ; i >= ; --i) ; j < i ...
- BZOJ 4547: Hdu5171 小奇的集合
Sol 首先,考虑这个要怎么搞...让总和最大的方法就是选出当前集合中最大的两个数相加放入集合中就可以了,证明非常简单,当前集合的和为x,它的和只会一直往后增加,所以只需要找到最大的两个数的和加入便是 ...
- 数据库多对多关联表(Python&MySQL)
Python Python对MySQL数据库操作使用的是sqlalchemy这个ORM框架 #一本书可以有多个作者,一个作者又可以出版多本书 from sqlalchemy import Table, ...
- python gui之tkinter界面设计pythonic设计
ui的设计,控件id的记录是一件比较繁琐的事情. 此外,赋值和读取数据也比较繁琐,非常不pythonic. 有没有神马办法优雅一点呢?life is short. 鉴于控件有name属性,通过dir( ...
- @ResponseBody返回不能正确接收
Spring-MVC中@ResponseBody返回Bean到前台接收这么一串代码,还套着HTML标签: The resource identified by this request is only ...
- 2017/1/7 学习笔记 jar包,maven
① 关于tar,jar,war文件 tar是通用的另一种打包格式,为了部署到服务器时方便. jar是java app server识别的java部署格式,其实是Zip文件,只是内部的文件有规范. wa ...
- POI文件导出至EXCEL,并弹出下载框
相关参考帖子 : [1]http://www.tuicool.com/articles/MnqeUr [2]http://www.oschina.net/question/253469_51638?f ...
- 解读Unity中的CG编写Shader系列二
转自 http://www.itnose.net/detail/6095974.html 上一篇文章的例子中我们可以看到顶点着色器的输出参数可以说是直接作为了片段着色器的形参传递过来,那么不由得一个问 ...
- fastReport 运行时设计报表 (mtm)
设计报表 通过“TfrxReport.DesignReport”方法调用报表设计器.你必须在你的项目中包含报表设计器 (必要条件是:要么使用“TfrxDesigner”组件,要么增加“frxDesgn ...
- win8内置管理员用户无法激活此应用
在运行中输入:“gpedit.msc”,就会启动组策略编辑器, 计算机配置 --> Windows设置 --> 安全设置 --> 本地策略 --> 安全选项 :::: 用 ...