长和双重赋值不是原子 - 它是怎么回事? [英] long and double assignments are not atomic - How does it matter?

查看:184
本文介绍了长和双重赋值不是原子 - 它是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道long和double赋值在Java中是不是原子的,直到它们被声明为volatile。我的问题是,它在我们的编程实践真的很重要。

We know that long and double assignments are not atomic in Java until they are declared volatile. My question is how does it really matter in our programming practice. for instance if you the see below classes whose objects are being shared among multiple threads.

/**
*  The below class is not thread safe. the assignments to int values would be 
*  atomic but at the same time it not guaranteed that changes would be visible to 
*  other threads.
**/
public final class SharedInt {

   private int value;

   public void setValue(int value) {
      this.value = value;
   }

   public int getValue() {
      return this.value;
   }

}

现在考虑另一个SharedLong

Now consider another SharedLong

/**
* The below class is not thread safe because here the assignments to  long 
*  are not atomic as well as changes are not
*  guaranteed to be visible to other threads.
*/
public final class SharedLong {

    private long value;

    public void setValue(long  value) {
      this.value = value;
    }

    public long getValue() {
       return this.values;
    }
}

现在我们可以看到上述两个版本不是线程安全。在 int 的情况下,这是因为线程可能会看到过期的整数值。而在 long 的情况下,他们可以看到损坏以及过期的long变量值。

Now we can see the both of the above versions are not thread safe. In case of int, it is because threads may see stale values of integer. While in case if long, they can see corrupt as well as stale values of long variable.

在这两种情况下,如果实例在多个线程之间不共享,那么类是安全的

In both cases, if an instance is not shared among multiple threads, then the classes are safe.

为了使上述类线程安全,我们需要声明 int和long都是volatile或使方法同步
这让我很奇怪:如果赋值 long double 我们的正常编程过程,因为两个都需要声明volatile或同步多线程访问,所以我的问题是长分配不是原子的事实可能会产生影响的情况。

To make the above classes thread safe we need to declare int and long both to be volatile or make the method synchronized. This make me wonder: How does it really matter if assignments to long and double are not atomic during our normal course of programming because both need to be declared volatile or synchronized for multithreaded access so my Questions is What are the scenarios where the fact that long assignments are not atomic may make a difference?.

推荐答案

使用 int 进行不当编程可能会导致观察到不正确的值使用 long 进行编程可能会导致观察不到实际存在的值。

Where improper programming with an int may result in stale values being observed, improper programming with a long may result in values that never actually existed being observed.

只需要最终正确,而不是时间点正确,所以跳过同步的性能。虽然为了表演而跳过一个挥发性的字段声明似乎偶尔的检查,如愚蠢。

This could theoretically matter for a system that only needs to be eventually-correct and not point-in-time correct, so skipped synchronization for performance. Although skipping a volatile field declaration in the interest of performance seems on casual inspection like foolishness.

这篇关于长和双重赋值不是原子 - 它是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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