Java Concurrent happens-before
happens-before relation on memory operations such as reads and writes of shared variables. The results of a write by one thread
are guaranteed to be visible to a read by another thread only if the write operation happens-before the read operation.
The synchronized and volatile constructs, as well as the Thread.start() and Thread.join()methods, can form happens-before
relationships. In particular:
- Each action in a thread happens-before every action in that thread that comes later in the program's order.
- An unlock (
synchronizedblock or method exit) of a monitor happens-before every subsequent lock (synchronizedblock or method
entry) of that same monitor. And because the happens-before relation is transitive, all actions of a thread prior to unlocking
happen-before all actions subsequent to any thread locking that monitor.
- A write to a
volatilefield happens-before every subsequent read of that same field. Writes and reads ofvolatilefields have similar
memory consistency effects as entering and exiting monitors, but do not entail mutual exclusion locking.
- A call to
starton a thread happens-before any action in the started thread.
- All actions in a thread happen-before any other thread successfully returns from a
joinon that thread.
The methods of all classes in java.util.concurrent and its subpackages extend these guarantees to higher-level synchronization.
In particular:
- Actions in a thread prior to placing an object into any concurrent collection happen-before actions subsequent to the access or
removal of that element from the collection in another thread.
- Actions in a thread prior to the submission of a
Runnableto anExecutorhappen-before its execution begins. Similarly forCallables
submitted to an ExecutorService.
- Actions taken by the asynchronous computation represented by a
Futurehappen-before actions subsequent to the retrieval of the
result via Future.get() in another thread.
- Actions prior to "releasing" synchronizer methods such as
Lock.unlock,Semaphore.release, andCountDownLatch.countDownhappen-before
actions subsequent to a successful "acquiring" method such as Lock.lock, Semaphore.acquire, Condition.await, and CountDownLatch.await on
the same synchronizer object in another thread.
- For each pair of threads that successfully exchange objects via an
Exchanger, actions prior to theexchange()in each thread
happen-before those subsequent to the corresponding exchange() in another thread.
- Actions prior to calling
CyclicBarrier.awaithappen-before actions performed by the barrier action, and actions performed by the barrier
action happen-before actions subsequent to a successful return from the corresponding await in other threads.
Java Concurrent happens-before的更多相关文章
- java concurrent包的学习(转)
java concurrent包的学习(转) http://my.oschina.net/adwangxiao/blog/110188 我们都知道,在JDK1.5之前,Java中要进行业务并发时,通常 ...
- How to Create a Java Concurrent Program
In this Document Goal Solution Overview Steps in writing Java Concurrent Program Template ...
- Java Concurrent Topics
To prevent Memory Consistency Errors(MCEs), it is good practice to specify synchronized class specif ...
- Java中编写线程安全代码的原理(Java concurrent in practice的快速要点)
Java concurrent in practice是一本好书,不过太繁冗.本文主要简述第一部分的内容. 多线程 优势 与单线程相比,可以利用多核的能力; 可以方便的建模成一个线程处理一种任务; 与 ...
- Java Concurrent之 AbstractQueuedSynchronizer
ReentrantLock/CountDownLatch/Semaphore/FutureTask/ThreadPoolExecutor的源码中都会包含一个静态的内部类Sync,它继承了Abstrac ...
- [Java Concurrent] 多线程合作 producer-consumers / queue 的简单案例
在多线程环境下,通过 BlockingQueue,实现生产者-消费者场景. Toast 被生产和消费的对象. ToastQueue 继承了 LinkedblockingQueue ,用于中间存储 To ...
- [Java Concurrent] 多线程合作 wait / notifyAll 的简单案例
本案例描述的是,给一辆汽车打蜡.抛光的场景. Car 是一辆被打蜡抛光的汽车,扮演共享资源的角色. WaxOnCommand 负责给汽车打蜡,打蜡时需要独占整部车,一次打一部分蜡,等待抛光,然后再打一 ...
- [Java Concurrent] 并发访问共享资源的简单案例
EvenGenerator 是一个偶数生成器,每调用一个 next() 就会加 2 并返回叠加后结果.在本案例中,充当被共享的资源. EvenChecker 实现了 Runnable 接口,可以启动新 ...
- 利用java concurrent 包实现日志写数据库的并发处理
一.概述 在很多系统中,往往需要将各种操作写入数据库(比如客户端发起的操作). 最简单的做法是,封装一个公共的写日志的api,各个操作中调用该api完成自己操作日志的入库.但因为入数据库效率比较低,如 ...
- [Java concurrent][Collections]
同步容器类 同步容器类包括Vector和Hashtable,二者是早期JDK的一部分.以及一些在JDK1.2中添加的可以由Collections.synchronizedXxx等工厂方法创建的. 这些 ...
随机推荐
- python学习之find()
定义: find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1. 语法 ...
- jsp tld 的tag 自定义标签扩展
引入方式 <%@ taglib prefix="bgt" uri="/WEB-INF/tlds/bgt.tld" %> 写法示例如下: <?x ...
- strncmp----c++库函数
某天,蒜头君和花椰妹在公园里散步,走着走着,我的天!他们各自都捡到了一串漂亮的字符串,然而蒜头君好奇心比较重,他想知道自己的字符串在花椰妹的字符串中出现了多少次,例如花椰妹的字符串为abababa,蒜 ...
- 八大CMS内容管理系统推荐
cms系统哪个好 感谢 64320 的投递 时间:2015-03-05 来源:http://www.iisp.com/ztview/ID_16129.html?s=bios 耐思尼克 很多新手站长初次 ...
- 海康威视监控设备的Yv12视频在XNA框架中播放
最近使用海康威视的5寸一体化智能球,查阅些SDK, 在Winform中海康威视能较好的集成,但是对于Yv12编解码和实时视频流的支持未找到较好的SDK介绍. 因为项目需要是用在XNA游戏框架中,本身有 ...
- MapReduce实战(五)实现关联查询
需求: 利用MapReduce程序,实现SQL语句中的join关联查询. 订单数据表order: id date pid amount 1001 20150710 P0001 2 1002 20150 ...
- 【Mac + GitHub】之在另一台Mac电脑上下载GitHub的SSH链接报错
当输入git命令github项目时报错: ⇒ git clone git@github.com:/TX-Class.git Cloning into 'TX-Class'... Warning: Pe ...
- 对hibernate的延迟加载如何理解,在实际应用中,延迟加载与session关闭的矛盾是如何处理的?
对hibernate的延迟加载如何理解,在实际应用中,延迟加载与session关闭的矛盾是如何处理的? 解答:延迟加载就是并不是在读取的时候就把数据加载进来,而是等到使用时再加载.那么Hibernat ...
- web.config配置文件中的configSource属性
每个网站里都会有一个web.config文件.修改Web.config文件会导致IIS重启,就是随意的回车一下也会导致重启.微软建议,不要将需要修改的配置内容保存在web.config中.而是单独放在 ...
- Charlse 抓包
下载Charlse:https://www.charlesproxy.com/ 安装使用: https://www.cnblogs.com/ceshijiagoushi/p/6812493.html ...