My question today is regarding multidimensional arrays, and synchronization.. I'm working on a client/server project and I'm not sure about the theory of an idea I came up with:
I have an array: Object[][] myArray = new Object[256][];
It is an array of arrays. The length of myArray is 256 and will never change from 256. However, the length of the second level of arrays is dynamic, and each second-level array can be null, zero-length, or any other length.
What I need to do, since multiple threads work with myArray simutaneously, is synchronize the array during possible changes to the second-level arrays. Anywhere an access/modification is made, myArrays is synchronized. synchronized (myArray) { ... }
The problem is that synchronize causes threads to block, even when they wish to read or write to a different second-level array than the one already being processed. What I want to do is lock only the specific, second-level array, and leave the rest of the arrays unlocked so the program can function more quickly.
Can I do this? synchronized (myArray[indexToLock]) { ... }
I can ensure that the second-level arrays are never null, rather zero-length or more. NetBeans lets me write the code, but to test it...
Any thoughts? Thank youuu! ;)