Reputation: 75
How to design a 2D array in java in such a way that it allows multiple threads to modify or insert value at a particular position without using synchronization
Upvotes: 5
Views: 2708
Reputation: 3119
There are several ways to do it.
Firstly, if you want maximum concurrency as a common library regardless of how many threads are accessing the matrix, and your int[][]
is reasonably small, you can do like this.
AtomicBoolean
equals to the dimension of the int[][]
.AtomicBoolean
first, the use Unsafe.putIntVolatile()
to update the value in volatile way, then unluck the AtomicBoolean
.Secondly, if you know the number of threads in advance.
Unsafe.compareAndSet()
first; then update it by Unsafe.putIntVolatile()
; and finally unmark the marker matrix by reset the value to -1.Lastly, if you can tolerate the int[][]
isn't really an int matrix in the actual code, what you can do is to use a long[][]
matrix, where the first 32 bit is the current number of updates to the value and the last 32 bit is the current value. Then you can easily use Unsafe.compareAndSet()
to set this long value as a whole. Note you can circular the upper 32 bit value so you don't have to have a update upper limit == Integer.MAX_VALUE
.
Upvotes: 2
Reputation: 8128
If you can guarantee that each element of the array(s) is only ever accessed by a single thread (until you completed the computation that can change the array(s) contents), then you can safely use many threads. (because they will be effectively independent) Otherwise, it's probably a bad idea…
Upvotes: 1
Reputation: 16628
Create a static
variable that can be accessed by all your Thread
s like:
public class MainClass {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
public void run() {
Constants.intValue[0][0] = new Integer(10);
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
Constants.intValue[0][1] = new Integer(20);
}
});
Thread t3 = new Thread(new Runnable() {
public void run() {
for (Integer[] valueArr : Constants.intValue) {
for (Integer intValue : valueArr) {
System.err.println(intValue);
}
}
}
});
t1.start();
t2.start();
t3.start();
}
}
interface Constants {
Integer[][] intValue = new Integer[2][2];
}
The only case will be next thread will override previous set value if it is using the same array position.
Upvotes: 0
Reputation: 39631
Does this question make sense? If we assume an int[][]
all operations on it will be atomic so there should be no need for synchronization! But what confuses me on the question: you cannot insert values in means of changing the array bounds after you have declared it.
Upvotes: 1
Reputation: 1902
Well you cannot do it without synchronization. Only thing you can do is reduce the scope of he lock used. If you don't know yet, read about lock coarsening. ConcurrentHashMap uses it. Idea is that instead of locking the whole array to modifications you just lock a segment of array (e.g: start, middle or end) where the modification would happen. This keeps the array DS open for reads and writes by other threads and no blocking happens unless 2 threads try to mutate the same segment simultaneously.
Upvotes: 2