本文介绍了java原子操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

分割以下陈述是正确的:

Is correct to divide the following statements:

int v = ++j;

as:


  1. 读取j值(原子);

  2. 将读取的值增加1(NON
    原子可能受到
    其他线程干扰);

  3. 将添加结果写入i
    (原子);

  4. 将i写入v(原子)

  1. read j value (atomic);
  2. increment by 1 the value read (NONatomic possibly interference byother thread);
  3. write the adding result to i(atomic);
  4. write i into v (atomic)


推荐答案

是, int (或更小的数据类型)读/写/算术运算是原子的。引用(读/写)也是原子的,无论它是32位还是64位。

Yes, int (or smaller datatypes) read/write/arithmetic operations are atomic. References (read/write) are also atomic, regardless of whether it's 32-bit or 64-bit.

但是,64位上的操作long double 可能不是原子的。

However, operations on 64-bit long and double may not be atomic.

出于Java编程语言内存模型的目的,一次写入非 volatile long double 值被视为两个单独的写入:每个32位半写一个。这可能导致线程从一次写入看到64位值的前32位,而从另一次写入看到第二次32位的情况。写入和读取 volatile long double 值始终是原子的。写入和读取引用始终是原子的,无论它们是实现为32位还是64位值。

For the purposes of the Java programming language memory model, a single write to a non-volatile long or double value is treated as two separate writes: one to each 32-bit half. This can result in a situation where a thread sees the first 32 bits of a 64 bit value from one write, and the second 32 bits from another write. Writes and reads of volatile long and double values are always atomic. Writes to and reads of references are always atomic, regardless of whether they are implemented as 32 or 64 bit values.

鼓励VM实现者避免拆分其64位值在可能的情况。建议程序员将共享的64位值声明为 volatile 或正确同步其程序以避免可能的复杂情况。

VM implementors are encouraged to avoid splitting their 64-bit values where possible. Programmers are encouraged to declare shared 64-bit values as volatile or synchronize their programs correctly to avoid possible complications.

请注意,前后增量/减量运算符本身都不是原子的,甚至不是 int byte :读/写/算术运算以明显不同的步骤发生。

Note that neither pre- nor post- increment/decrement operators themselves are atomic, not even on int or byte: the read/write/arithmetic operations happens in distinctly separate steps.



  • Java Tutorials/Essentials/Concurrency/Atomic Variables

这篇关于java原子操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 11:15