Im working on a java project for school where you have to deal with a multithreaded program. I need a class, shared between many threads that is basically a matrix of values that have to manage concurrent access, it looks something like:
public class CacheMatrix{
private MyType[][] cachedItems;
public CacheMatrix(int size){
this.cachedItems = new MyType[size][size];
}
public MyType readItem(int x, int y){...}
public void storeItem(int x, int y, MyType item){...}
}
I was about to manage it using a matrix of ReentrantReadWriteLock, one for each item, but i realized that the matrix size is around 10^3 X 10^3 so i would have 1 million lock!
Do you think this is the way to go? (is it ok to create so many locks?)
Can you find a better way that preserve almost only the minimum mutual exclusion but uses less lock, considering that the number of threads that use this class is limited to a small number N (N range from 2 to 8)?
Thanks fro your support!