简体   繁体   English

在Java中建立ig事前关系

[英]Establishig happens-before relationship in Java

I know, that there are two way to establish happens-before relationship in java: synchronized blocks and methods, volatile keyword. 我知道,有两种方法可以在Java中建立先发生后关系:同步块和方法,volatile关键字。 (if i'm correct, it doesn't work with final fields). (如果我是正确的,则不适用于最终字段)。 My question is: do atomic variables from concurrent package behave alike? 我的问题是:并发包中的原子变量的行为是否相似? can happens-before be established by them? 可以在他们建立之前发生吗?

yes, they are thread safe and thus you can determine happens/before and after relationships. 是的,它们是线程安全的,因此您可以确定发生关系之前/之后。

AtomicXxxx class operations use volatile, CAS, atomic add or lazy/unordered set. AtomicXxxx类操作使用易失性,CAS,原子加法或惰性/无序集合。 All have some thread safety guarantees but are more light weight than synchronized. 它们都具有一定的线程安全保证,但重量轻于同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM