简体   繁体   English

Java volatile变量,多线程

[英]Java volatile variable, multi threading

I have an application which are multi threading. 我有一个多线程的应用程序。 I notice some existing code use volatile when the variable is shared by several threads. 当多个线程共享该变量时,我注意到一些现有代码使用volatile。 Why not just use synchronized in the method when variable is used, what's the benefits to define variable as volatile? 为什么不只在使用变量时在方法中使用同步,将变量定义为volatile有什么好处?

Declaring a volatile Java variable means: 声明一个易失的Java变量意味着:

  • The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory"; 该变量的值永远不会在线程本地缓存:所有读取和写入操作都将直接进入“主内存”。
  • Access to the variable acts as though it is enclosed in a synchronized block, synchronized on itself. 对变量的访问就好像它被封装在一个同步块中一样,它本身是同步的。

In other words, the main differences between synchronized and volatile are: 换句话说,同步和易失性之间的主要区别是:

  • a primitive variable may be declared volatile (whereas you can't synchronize on a primitive with synchronized); 原始变量可能被声明为volatile(而您不能在已同步的原始变量上进行同步);
  • an access to a volatile variable never has the potential to block: we're only ever doing a simple read or write, so unlike a synchronized block we will never hold on to any lock; 对volatile变量的访问永远不会阻塞:我们只做简单的读取或写入操作,因此与同步块不同,我们永远不会保持任何锁;
  • because accessing a volatile variable never holds a lock, it is not suitable for cases where we want to read-update-write as an atomic operation (unless we're prepared to "miss an update"); 因为访问易失性变量永远不会持有锁,所以它不适用于我们希望以原子操作方式进行读取-更新-写入的情况(除非我们准备“错过更新”);
  • a volatile variable that is an object reference may be null (because you're effectively synchronizing on the reference, not the actual object). 作为对象引用的volatile变量可能为null(因为您实际上是在引用上而不是实际对象上进行同步)。

more information is: http://javamex.com/tutorials/synchronization_volatile.shtml 更多信息是: http : //javamex.com/tutorials/synchronization_volatile.shtml

volatile is much simpler and faster than using synchronized. 与使用同步相比,volatile更简单,更快捷。

As it is simpler it has limited uses, but if volatile is all you need, why use synchronized. 因为它比较简单,所以用途有限,但是如果您只需要volatile,那么为什么要使用同步。 ;) ;)

Taken from here : 这里拍摄:

  • A primitive variable may be declared volatile (whereas you can't synchronize on a primitive with synchronized); 原始变量可能被声明为volatile(而您不能在已同步的原始变量上进行同步);

  • An access to a volatile variable never has the potential to block: we're only ever doing a simple read or write, so unlike a synchronized block we will never hold on to any lock; 对volatile变量的访问永远不会被阻塞:我们只做简单的读取或写入操作,因此与同步块不同,我们永远不会保持任何锁。

  • Because accessing a volatile variable never holds a lock, it is not suitable for cases where we want to read-update-write as an atomic operation (unless we're prepared to "miss an update"); 因为访问易失性变量永远不会持有锁,所以它不适用于我们希望以原子操作方式进行读取-更新-写入的情况(除非我们准备“错过更新”);

  • A volatile variable that is an object reference may be null (because you're effectively synchronizing on the reference, not the actual object). 作为对象引用的volatile变量可能为null(因为您实际上是在引用而不是实际对象上进行同步)。

In short, we should use volatile only at least when we have one variable (state) shared among threads. 简而言之,至少在线程之间共享一个变量(状态)时,才应使用volatile Less costlier than synchronized , less intuitive too. synchronized相比,它的成本less intuitiveless intuitive There are more than one variables holding the state volatile is a problem. 有多个变量使状态易变是一个问题。

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

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