Is there a way to declare array elements volatile
in Java? I.e.
volatile int[] a = new int[10];
declares the array referen
How about this:
static class Cell {
volatile T elem;
}
private Cell[] alloc(int size){
Cell[] cells = (Cell[]) (new Cell[size]);
return cells;
}
volatile Cell[] arr;
Cell[] newarr = alloc(16);
for (int i = 0; i < newarr.length; i++) {
newarr[i] = new Cell<>();
}
arr = newarr;
the cells make the content volatile too. also I assign the new array to the volatile one only after pre allocating the cells... there's the trade off of the Cell extra memory, but it's manageable