Java array synchronization

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.

+3
source share
3 answers

int [] [] [] - , ( [A] [B]) , , .

synchronized (tasks [A]), , - . , , , ( [A]), , , , , , , .

, ! , , .

, ( ).

, , . .

+2

. ( ) ; tasks[A] tasks[A][B]. , "" tasks[A], , synchronized (tasks[A]). - (, tasks[A][B]), . '

, - - " , , concurrency?" , . .

+4

, , , , ; - ? , , ?

, , , , / tasks. (, , ) , java.util.concurrent , . , , . , , , , , , 3d-jagged-array.

+1

Source: https://habr.com/ru/post/1793424/


All Articles