简体   繁体   English

Java内存模型是否允许重新排序许多原子/易变量的非同步访问?

[英]Will the Java Memory Model permit the reordering of non-synchronized accesses of many atomic/volatile variables?

I want to know if the JMM will permit an implementation to reorder the access to the ai and ada variables to behave differently than the intention shown in the code. 我想知道JMM是否允许实现重新排序对aiada变量的访问,使其行为与代码中显示的意图不同。

The intention is that the method perform ops is the next: 目的是方法执行ops是下一个:

  1. gets the next index of an array of volatile items 获取易失物品数组的下一个索引
  2. writes a number in the newly obtained index 在新获得的索引中写入一个数字
  3. performs a spinlock waiting to be sure that older indexes of the array got it's value 执行一个自旋锁,等待确保数组的旧索引得到它的值
  4. prints the sum of the array from the first index to the current obtained one. 打印从第一个索引到当前获得的索引的数组之和。

What happens after the index reached 1000 is non-important to me. 索引达到1000后会发生什么对我来说并不重要。 I actually want to use the array as a ring, but if this question gets answered I will be able to figure out how to do it. 我实际上想要将数组用作环,但如果这个问题得到解答,我将能够弄清楚如何做到这一点。

Basically I want to avoid locks and rely on atomic and lock-free objects. 基本上我想避免锁定并依赖原子和无锁对象。 I don't know however if in this specific case I still need implicit synchronization. 然而,我不知道在这种特定情况下我是否还需要隐式同步。

AtomicInteger ai = new AtomicInteger(0);
// let's say all values are null by default
AtomicDoubleArray ada = new AtomicDoubleArray(1000); 
int rn = 4; // dice rolled...

void perfomOps(AtomicInteger ai, AtomicDoubleArray ada) {
   int i = ai.getAndIncrement();
   ada.set(i, rn); 
   spinlock(ada, i);
   printSum(ada, i);
}

void spinlock(AtomicDoubleArray ada, int idx) {
// spinlock in case the other threads couln't write to older indexes yet
   if (idx > 0)
      for (int c = 0;c < idx; c++) 
         while (i = ada.get(c) == null);
}

void printSum(AtomicDoubleArray ada, int idx) {
   double sum = 0;
   for (int i = 0;i < idx; i++)
      sum = sum + ada.get(i);
   Sytem.out.println(sum);
}

// thread 1
perfomOps(ai, ada); // lets say we get 4 printed

// thread 2
perfomOps(ai, ada); // lets say we get 8 printed

// thread 3
perfomOps(ai, ada); // lets say we get 12 printed

Other threads will keep doing the same, it can happen that the print order is not as expected, like 8, 12 and then 4. But assuming a next thread comes in, then it will see correct values and correctly print now 16. 其他线程将继续这样做,可能会发生打印顺序不符合预期,如8,12和然后4.但假设下一个线程进来,那么它将看到正确的值并正确打印16。

All this fancy stuff is to avoid making explicit locks in order to measure which one performs better, the lockfree version or the synchronized one 所有这些花哨的东西都是为了避免显式锁定以测量哪一个更好,无锁版本或同步版本

The most important rule of the Java Memory Model is (§17.4.5): Java内存模型最重要的规则是(§17.4.5):

A program is correctly synchronized if and only if all sequentially consistent executions are free of data races. 当且仅当所有顺序一致的执行没有数据争用时,程序才能正确同步 If a program is correctly synchronized, then all executions of the program will appear to be sequentially consistent (§17.4.3). 如果程序正确同步,则程序的所有执行都将显示为顺序一致(第17.4.3节)。

If all shared variables are volatile or atomic , then there are no data races. 如果所有共享变量都是volatileatomic ,那么就没有数据争用。 That means sequential consistency is guaranteed. 这意味着保证了顺序一致性 And that means, there will be no reorderings visible in the behaviour of the program. 这意味着,程序的行为中不会出现重新排序。

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

相关问题 Java内存模型,易失性和同步块访问非易失性变量 - Java Memory Model, volatile, and synchronized blocks accessing non-volatile variables 在读写原子性、可见性和防止重新排序方面,锁定、同步、原子变量与 Java 中的 volatile - Lock vs synchronized vs atomic variables vs volatile in java in terms of read-write atomicity, visibility and protection from reordering Java内存模型中的同步和易失性如何工作? - How synchronized and volatile work in Java memory model? java-等待并在非同步块中进行通知 - java - wait and notify in a non-synchronized block java 同步监视器进入对非同步访问或非易失性变量的影响 - Effect of java synchronized monitor enter on non synchronized access or not volatile variables Java 中同步和非同步集合类有什么区别? - What is the difference between synchronized and non-synchronized collection classes in Java? Java用非易失性对易失性写进行重新排序 - Java reordering volatile write with non-volatile 使用Java并行流在非同步ArrayList中添加元素 - Adding elements in Non-synchronized ArrayList using java parallel stream 非同步的WeakHashMap是否有害? - Is a non-synchronized WeakHashMap harmful? 了解 Java 中同步块与 volatile 变量的原子性、可见性和重新排序 - Understanding atomic-ness, visibility and reordering of synchonized blocks vs volatile variables in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM