check then act and read modify write
来源:互联网 发布:车机安卓软件 apk 编辑:程序博客网 时间:2024/04/29 22:52
翻译自stackoverflow:
问题:
例1:check then act
if(vector.contains(e))//check{vector.remove(e)}
这有2个线程,线程1在contains后暂停,然后线程2执行了contains并且执行了remove,这时e已经不在vector中了,
然后线程1取消暂停并执行remove,此时报错,因为这时e已经不在vector中了。
例2:read modify write
counter++
这不是自动的操作, 他有3个步骤:1. 取得该值2. 增加该值3. 归属该值---------分割线---------------------
经典回答:
关于read modify write的经典例子就是上述的counter++了。
关于check then act 的例子很多,一个例子就是当你从ConcurrentHashMap中检查键是否存在然后在if中做些事情(译者注:此例子在书中提到过,网上也有分析,我这里贴出来大家自己思考):
继续翻译:
另一个例子是单例代码:
public Singleton getInstance(){ if(_instance == null) { _instance = new Singleton(); }}
(译者注:这个一眼就看出来这个单例是有问题的,此处应该加锁,否则可能并发时new出多个实例)你能在网上读到更多的关于这方面的知识,目前的一本好书是《Java Concurrency in Practice》,作者Brian Goetz。这本书也是有帮助的。
0 0
- check then act and read modify write
- C# modify file (read all and modify and write all)
- read numbers from file and exchange their positions then write into another file
- Read and Write XML
- Read and Write Excel
- file read and write
- write and read
- read and write files
- Python read and write
- C# Read and Modify XML File
- Increment V.S. Read-Modify-Write in HBase
- [hbase]Increment V.S. Read-Modify-Write in HBase
- read and write binary xml
- Read and write bytes data
- Read and Write File Code
- read and write '.bmp' file
- QT之write and read
- IO read and write. demo
- 数字
- 特征值与特征向量理解
- intellij idea 快速上手指南
- Ubuntu/UbuntuKylin平台下源码编译Ambari 2.4(含较多问题解决方案)
- android app还原出厂设置
- check then act and read modify write
- linux inotifywait
- smali 学习笔记 1)
- 从零开始,学习web前端之CSS3
- cat <<EOF与cat <<-EOF的区别
- 分布式锁
- 图像处理-线性滤波-3 高斯滤波器
- 树莓派2 Rapsbian 升级后不能正确识别USB接口的网卡和鼠标键盘
- gcc+nodejs+python+mongdb+django