multithreading - Java synchronized references -


i have class , b.

public class a() {      private static list<int> list = new arraylist<int>();      public static list<int> getlist() {         return list;     } }  public class b() {      public void foo() {         synchronized(a.getlist()) {             // stuff         }     } } 

in class b synchronize. synchronize on a's list, or on b's reference a's list. think latter use help.

if how accomplish similar thing work?

thanks!

the op followed comment:

what's weird behavior seeing if lock within can lock @ same time in b. pretty means synchronized has no effect. why surmised maybe locking reference won't work.

that can't happen code in question.

but happen if there setlist method in a updated list variable. end in situation 2 threads locking on different objects.

another possibility have 1 thread taking lock on same object twice. that's normal behavior. java primitive mutexes reentrant; i.e. prevent 2 different threads holding same mutex @ same time.


Comments