Java中变量读写的原子性 [英] Atomicity of Reads and Writes for Variables in Java

查看:283
本文介绍了Java中变量读写的原子性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是另一个我的问题

@templatetypedef回答了问题(赞赏),并在他的回答中写道:

@templatetypedef answered the question (appreciated), and in his answer he wrote:


作为注释 - 原子性并不意味着所有其他线程将被阻塞
,直到值准备好。这意味着所有其他线程将看到
纯粹在操作完成之前或完全在
操作完成之后状态,但没有别的。

As a note - atomicity does not mean "all other threads will be blocked until the value is ready. It means all other threads will either see the state purely before the operation is done or purely after the operation is done, but nothing else.

我有一个混乱关于这个,这就是原因:

I have a confusion regarding this, and here’s why:

它说这里


原子动作不能交错,因此可以毫不费力地使用它们b $ b线程干扰。

Atomic actions cannot be interleaved, so they can be used without fear of thread interference.

我从中推断,它与他写的内容相矛盾。

What I infer from this, is that it contradicts what he wrote.

如果我们有2个int变量i1和i2,我们进行原子操作 i1 = i2; 并且此操作由threadX执行。

If we have 2 int variables i1 and i2, and we do the atomic operation i1=i2; and this operation is executed by threadX.

然后,如果原子动作不能如上所述交错,则意味着在此原子操作期间(由threadX执行),不允许其他threadY访问(对于任何一个读取)或写)相同的变量i2,因此,没有其他threadY,在原子操作期间允许访问同一个变量,因此存在某种形式的阻塞。

Then if atomic actions cannot be interleaved as indicated above, it means that during this atomic operation (executed by threadX), no other threadY is allowed to access (for either read or write) that same variable i2, hence, no other threadY, is allowed to access that same variable during the atomic operation, so some form of blocking does exist.

我明白了吗?

谢谢...

推荐答案

据我所知,没有原子 i1 = i2 操作。你可以自动读取一个int,你可以原子地写入一个,但是你不能在同一个同步操作中同时执行这两个操作。所以 i1 = i2 是两个不同的原子操作,一个读操作然后写一个。您可以确保没有任何内容可以交错读取操作,因此当您阅读它时,您将看不到 i2 的部分更新,并且您可以保证不会将写入交错到 i1 ,但不保证在这两个原子操作之间不会发生任何事情。

To the best of my knowledge there is no atomic i1 = i2 operation. You can atomically read an int and you can atomically write to one, but you can't do both in the same operation with synchronization. So i1 = i2 is two different atomic operations, a read followed by a write. You are guaranteed that nothing will interleave the read operation so you won't see partial updates to i2 when you read it, and you are guaranteed that nothing will interleave the write to i1, but there's no guarantee that nothing will happen in between those two atomic operations.

让我们说线程t1正在进行要做的事情:

Lets say Thread t1 is going to do:

i2 = 10
i1 = i2

线程t2将会:

i1 = 7
i2 = 18
System.out.println(i1)

什么你保证是t1最终会分配10或18到i1,但你不知道哪个。但是,你可以保证它不能是任何其他值,因为读取i2和写入i1是原子的,所以你不能在修改它时看到i2的某些位。同样,t2保证打印10,18或7,它不能打印任何其他内容。但是,如果没有同步,就无法知道它最终会打印出哪3个值。

What you are guaranteed is that t1 will end up assigning either 10 or 18 to i1 but you can't know which. However, you are guaranteed it can't be any other value because the read of i2 and the write to i1 are atomic so you can't end up seeing some of the bits of i2 while it's being modified. Similarly, t2 is guaranteed to print either 10, 18, or 7 and it can't print anything else. However, without synchronization there's no way to know which of those 3 values it will end up printing.

这篇关于Java中变量读写的原子性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆