4

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?

6
  • 1
    You created an array of null's, you need to initialize every object: atomicArray[0][0] = new AtomicInteger();
    – alaster
    May 23, 2012 at 10:58
  • 5
    I don't want to be rude, but if you can't diagnose a NullPointerException, it's probably much too early to mess with concurrency/multithreading issues. And without the code, it's impossible to make the diagnostic for you.
    – JB Nizet
    May 23, 2012 at 10:59
  • @alaster thanks! I have another question (extended from the last) is because I will iterating through all 100 objects. and I'm only initializing 20-30 objects, how can I get my uninitialized objects to not come up with a nullexception? May 23, 2012 at 11:00
  • NullPointerException is thrown when you call array[someIndex] when this object is null. If you want to use it you must initialize it before. Or just don't call this object at all. You can use lazy initialize: if (array[someIndex] == null) { array[someIndex] = new AtomicInteger(); } But i think @JBNizet is right. AtomicInteger used in complicated code
    – alaster
    May 23, 2012 at 11:05
  • Thanks for your concern, but its for an assignment, so I don't really get a choice. May 23, 2012 at 11:22

2 Answers 2

6

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.

2

You need to instantiate all of the positions in the matrix before accessing them, something like this:

atomicArray[i][j] = new AtomicInteger();

Or this, if you want to initialize each atomic integer in a certain initial value:

atomicArray[i][j] = new AtomicInteger(initialValue);

That, for all i,j positions in the matrix. Normally you'd do this using a couple of nested for loops:

for (int i = 0; i < 100; i++) {
    for (int j = 0; j < 100; j++) {
        atomicArray[i][j] = new AtomicInteger();
    }
}
1
  • 3
    Thanks :) I now have the Game of life running in parallel :) May 24, 2012 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.