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 (
synchronized
block or method exit) of a monitor happens-before every subsequent lock (synchronized
block 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
volatile
field happens-before every subsequent read of that same field. Writes and reads ofvolatile
fields have similar
memory consistency effects as entering and exiting monitors, but do not entail mutual exclusion locking.
- A call to
start
on a thread happens-before any action in the started thread.
- All actions in a thread happen-before any other thread successfully returns from a
join
on 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
Runnable
to anExecutor
happen-before its execution begins. Similarly forCallables
submitted to an ExecutorService
.
- Actions taken by the asynchronous computation represented by a
Future
happen-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.countDown
happen-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.await
happen-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等工厂方法创建的. 这些 ...
随机推荐
- nyoj 975 Distinct Count
Distinct Count 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 给一个长度为 n 的数列 {an} ,找出有多少个长度为 m 的区间,使区间中不含有重复的数 ...
- Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Security policy requir
今天升级Xcode 7.0 bata发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport S ...
- 本地测试Tomcat配置Https访问
一.tomcat开启HTTPS配置 1) 准备证书 使用jdk工具keytool生成一个ssl测试用证书, 一路按照提示操作输入即可 keytool -genkey -alias tomcat -ke ...
- linux学习笔记8--命令touch
linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. touch命令有两个功能:一是用于把已存在文件的时间标签更新为系统当前的时间(默认 ...
- FirstLetterUtil
package cn.edu.hbcf.common.utils; public class FirstLetterUtil { private static int BEGIN = 45217; p ...
- C语言函数sscanf()的用法(转)
转自:http://www.cnblogs.com/lyq105/archive/2009/11/28/1612677.html C语言函数sscanf()的用法 sscanf() - 从一个字符串中 ...
- (转)RISC-V结构逻辑图
转载地址:http://blog.csdn.net/zzwu/article/details/54810162 说明: 执行6级流水作业: 1. fetch(取指) 2. decode(译码) 3. ...
- hdu6006 Engineer Assignment 状态dp 定义dp[i][s]表示前i个工程状态为s可以执行的最大工程数。s表示前i个工人选走了s状态的工程师。
/** 题目:hdu6006 Engineer Assignment 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6006 题意:已知n个工程,每个需要某 ...
- MapReduce实战(二)自定义类型排序
需求: 基于上一道题,我想将结果按照总流量的大小由大到小输出. 思考: 默认mapreduce是对key字符串按照字母进行排序的,而我们想任意排序,只需要把key设成一个类,再对该类写一个compar ...
- jquery Fancybox使用教程
Fancybox是一款基于jquery的对图片展示播放的插件,当然,它html文本.flash动画.iframe以及ajax也予以支持.还可以通过css自定义外观,阴影效果超级赞! 演示效果:http ...