I have an async task management class that has an array like this:
public static int[][][] tasks;
I basically refer to the cells as follows:
synchronized(tasks[A][B]) {
// Doing something with tasks[A][B][0]
}
My question is: if I do this:
synchronized(tasks[A]) {
}
will it also block threads trying to enter synchronized (tasks [A] [B])? In other words, synchronized array access also syncs accsess with its cells? If not, how do I block an array of WHOLE tasks [A] for my thread?
Edit: the answer is NO. When someone is in the synchronized block for tasks [A], someone can be in the synchronized block for tasks [A] [B] at the same time, for example, because it is a different object. Therefore, when it comes to accessing objects from one place at a time, arrays do not differ: touch an object X from one place at a time, when you need to surround it with synchronized (X) ALL, you touch it.
source
share