在 Java 中,如果每个线程写入单独的单元空间,是否需要同步对数组的写入访问? [英] In Java, is it required to synchronize write access to an array if each thread writes to a separate cell space?

查看:43
本文介绍了在 Java 中,如果每个线程写入单独的单元空间,是否需要同步对数组的写入访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果每个线程都写入一个单独的单元空间,是否需要在 Java 中同步对数组的写访问?

Is it required to synchronize write access to an array in Java if each thread writes to a separate cell space?

具体来说,该数组是原始数组或不可变对象数组.前任.一个 int 数组或一个 String 数组.

Specifically, the array is either a primitive Array or an immutable object array. Ex. An int array or a String array.

推荐答案

不,不需要同步.

它在 JLS §17.6 字词撕裂中定义一个>:

Java 虚拟机的一个实现考虑是每个字段和数组元素都被认为是不同的;对一个字段或元素的更新不得与任何其他字段或元素的读取或更新交互.尤其是,两个线程分别更新字节数组的相邻元素,不能相互干扰或交互,也不需要同步来保证顺序一致性.

One implementation consideration for Java virtual machines is that every field and array element is considered distinct; updates to one field or element must not interact with reads or updates of any other field or element. In particular, two threads that update adjacent elements of a byte array separately must not interfere or interact and do not need synchronization to ensure sequential consistency.

这篇关于在 Java 中,如果每个线程写入单独的单元空间,是否需要同步对数组的写入访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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