Volatile arrays in Java
Volatile arrays in Java
A slight complication of Java volatile fields, and one sometimes overlooked, is that declaring an array volatile does not give volatile access to its fields!. At least, it doesn't when elements of the array are accessed with "normal" Java syntax. In other words:
- it is unsafe to call arr[x] = y on an array (even if declared volatile) in one thread and then expect arr[x] to return y from another thread;
- on the other hand, it is safe to call arr = new int[] (or whatever) and expect another thread to then read the new array as that referenced by arr: this is what is meant by declaring the array reference volatile.
So, what do we do if we want a truly volatile array in Java, where the array's individual fields have volatile semantics?
Solution 1: use AtomicIntegerArray or AtomicLongArray
The AtomicIntegerArray class implements an int array whose individual fields can be accessed with volatile semantics, via the class's get() and set() methods. Calling arr.set(x, y) from one thread will then guarantee that another thread calling arr.get(x) will read the value y (until another value is read to position x).
Solution 2: re-write the array reference after each field write
This is slightly kludgy and slightly inefficient (since what would be one write now involves two writes) but I believe it is theoretically correct. After setting an element of the array, we re-set the array reference to be itself:
volatile int[] arr = new int[...];
...
arr[4] = 100;
arr = arr;
The marginal benefit of this technique could be:
- it may allow you to fix some broken code with minimal changes if you have code already (incorrectly) using a volatile array and expecting thread-safe element access;
- it saves us creating the wrapper object around the array (which is what happens with IntegerArray and LongArray);
- it's more or less the only option for array types with no available atomic wrapper (e.g. a float array).
Volatile arrays in Java的更多相关文章
- volatile关键字及Java内存模式
参考文档:https://www.cnblogs.com/_popc/p/6096517.html volatile关键字虽然从字面上理解起来比较简单,但是要用好不是一件容易的事情.它与Java的内存 ...
- java.util.Arrays,java.lang.Math,java.lang.System 类的常用方法汇总
java.util.Arrays类是数组的工具类,一般数组常用的方法包括 二分查找:public static int binarySearch(array[],int key),返回key的下标i ...
- leetcode第四题:Median of Two Sorted Arrays (java)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- java数组、java.lang.String、java.util.Arrays、java.lang.Object的toString()方法和equals()方法详解
public class Test { public static void main(String[] args) { int[] a = {1, 2, 4, 6}; int[] b = a; in ...
- LeetCode算法题-Intersection of Two Arrays(Java实现-四种解法)
这是悦乐书的第207次更新,第219篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第75题(顺位题号是349).给定两个数组,编写一个函数来计算它们的交集.例如: 输入: ...
- volatile变量,java内存模型
volatile变量提供了最轻量级的同步机制,当一个变量加上volatile修饰时,会具有一下两个特性 https://blog.csdn.net/u011277123/article/details ...
- coding++:Arrays.asList() - java.lang.UnsupportedOperationException异常处理
这个异常遇到了才知道坑这么大,坑爹的方法. private String[] otherUserFromArray = new String[]{“3”, “4”, “发放”}; List<St ...
- Median of Two Sorted Arrays LeetCode Java
两排序好的数组,找中位数 描述There are two sorted arrays A and B of size m and n respectively. Find the median of ...
- Java Arrays类进行数组排序
排序算法,基本的高级语言都有一些提供.C语言有qsort()函数,C++有sort()函数,java语言有Arrays类(不是Array).用这些排序时,都可以写自己的排序规则. Java API对A ...
随机推荐
- Myhchael原创题系列 Mychael vs Kid 【题解】
题目链接 Mychael vs Kid 题解 先说说这题的由来及前身 前身 首先有一个很经典的题目: 维护区间加,查询区间\(gcd\) 如果强行用线段树维护的话,区间加之后就没法直接确定当前区间的\ ...
- HDU 4549 矩阵快速幂+快速幂+欧拉函数
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- The Shortest Path in Nya Graph HDU - 4725
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- oepncv-学习笔记一
安装opencv文件时若需要cmake编译,如果中间出现 解决办法是: 在opencv的文件中找到包含cmakelist.txt的文件夹,把where is the source code:的路径改成 ...
- 管理页面的 setTimeout & setInterval
在管理 setTimeout & setInterval 这两个 APIs 时,笔者通常会在顶级(全局)作用域创建一个叫 timer 的对象,在它下面有两个数组成员 —— {sto, siv} ...
- 优化Hadoop Balancer运行速度
(如果运行hbase的话建议为16384),指定用于在DataNode间传输block数据的最大线程数,老版本的对应参数为dfs.datanode.max.xcievers 2.修改dfs.datan ...
- 浏览器发送URL的编码特性
转载自:http://blog.csdn.net/wangjun_1218/article/details/4330244 浏览器发送URL的编码特性 尽管有很多规范URL的标准,例如RFC 3987 ...
- 【Codeforces549F】Yura and Developers [单调栈][二分]
Yura and Developers Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample Input 4 ...
- bzoj 1965 数学
首先我们可以发现每张牌的对应关系,假设序号为x的牌,经过一次洗牌后的位置为: 2*x x<=n/2 2*(x-n/2)-1 x>n/2 那么我们可以将下面的式子化简,变成2*x-n ...
- SQLserver 字符串分割函数
CREATE function Get_StrArrayStrOfIndex ( @str varchar(), --要分割的字符串 @split varchar(), --分隔符号 @index i ...