The Java™ Tutorials — Concurrency :Atomic Access 原子访问

The Java™ Tutorials — Concurrency :Atomic Access 原子访问

原文地址:https://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html

关键点

  • 原子操作:不可打断的操作
  • 原子操作类型 
    • 对于引用变量,和大部分的原始数据类型的读写(除long 和 double外)操作都是原子的。
    • 对于所有被声明为volatile的变量的读写,都是原子的(除long和double外)
  • 可有效减少内存冲突,但是内存一致性问题没有得到缓解

全文翻译

In programming, an atomic action is one that effectively happens all at once. An atomic action cannot stop in the middle: it either happens completely, or it doesn’t happen at all. No side effects of an atomic action are visible until the action is complete.

在编程中,所谓的原子操作,是指实际上立刻发生的操作。原子操作是不能被中途打断的:它要么完成,要么根本就不发生。原子操作的副作用直到操作完成时才可见。

We have already seen that an increment expression, such as c++, does not describe an atomic action. Even very simple expressions can define complex actions that can decompose into other actions. However, there are actions you can specify that are atomic:

  • Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double).
  • Reads and writes are atomic for all variables declared volatile (including long and double variables).

我们之前已经见过自增表达式了,如 c++,但它并非一个原子操作。即使一个非常简单的表达式也可以定义一种可被分为好几个其他操作的复杂操作。不过,这里有几个你可以认定为原子操作的操作:

  • 对于引用变量,和大部分的原始数据类型的读写(除long 和 double外)操作都是原子的。
  • 对于所有被声明为volatile的变量的读写,都是原子的(除long和double外)

Atomic actions cannot be interleaved, so they can be used without fear of thread interference. However, this does not eliminate all need to synchronize atomic actions, because memory consistency errors are still possible. Using volatile variables reduces the risk of memory consistency errors, because any write to a volatile variable establishes a happens-before relationship with subsequent reads of that same variable. This means that changes to a volatile variable are always visible to other threads. What’s more, it also means that when a thread reads a volatile variable, it sees not just the latest change to the volatile, but also the side effects of the code that led up the change.

原子操作是不能交错进行的,所以对它们的使用是无需担心线程冲突问题的。然而,这并不意味着可以消除所有对原子操作的同步,因为内存一致性问题还是存在的。使用volatile变量可以减少内存一致性错误发生的风险,因为对volatile变量的任何写操作都会与随后对此变量的读操作间建立先行关系。这意味着,对一个volatile变量的更改总是对其他线程可见的。此外,这同样意味着,当一个线程读一个volatile变量时,它不仅仅可以获取其最新的更改,而且也可以看到代码更改所带来副作用。

Using simple atomic variable access is more efficient than accessing these variables through synchronized code, but requires more care by the programmer to avoid memory consistency errors. Whether the extra effort is worthwhile depends on the size and complexity of the application.

使用简单的原子变量访问要比那些通过同步代码块进行的访问要高效,但是这需要开发者更加小心内存一致性错误的发生。这种代价是否值得,取决于应用的大小和复杂性。

Some of the classes in the java.util.concurrent package provide atomic methods that do not rely on synchronization. We’ll discuss them in the section on High Level Concurrency Objects.

在java.util.concurrent包中的某些类提供了不依赖于同步的原子方法。我们会在《高并发对象》中讨论它们。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值