Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two Dimensional AtomicInteger array

Hey so I'm wondering how can I make AtomicInteger a two dimensional array, From what I've found on javadocs AtomicIntegerArray is only single dimension.

int[] newArray = new int[100];
AtomicIntegerArray atomicarray = new AtomicIntegerArray(newArray);

Which creates a AtomicIntegerArray of size 100. But I would like an atomicarray with two dimensions. I've tried doing..

AtomicInteger[][] atomicArray = new AtomicInteger[100][100];
atomicArray[00][00].set(1);

But I am met with..

java.lang.NullPointerException at nz.ac.massey.threadpool.MyClass.(MyClass.java:20)

So any ideas? thanks! :)... I haven't done much work with Atomic variables before.

if this isn't possible how can I minimic a regular primitive integer two dim array into a AtomicInteger two dim array?

like image 356
classicjonesynz Avatar asked Feb 20 '23 09:02

classicjonesynz


1 Answers

Just create a one-dimensional array of length m * n, you then need a function that maps a pair of integers (i, j) to one integer. i * n + j is a good start. Assuming m is the number of rows and n the number of columns.

It is a good idea to keep all of your integers inside the AtomicIntegerArray. Or you'll have to deal with concurrency your self.

like image 58
xiaofeng.li Avatar answered Feb 22 '23 22:02

xiaofeng.li