Good afternoon!
Inside class B, a method is defined using the keyword "synchronized". Inside this method, you work with an object of class A, which is publicly available. Tell me, please, is thread safety guaranteed for object A in this case? I.e. synchronized will be applied to a separate instance of B, not focusing on A - "synchronized(this { } == synchronized(B) { }"? For greater thread safety, is it necessary to use a block of the type "synchronized(A) { }"?
The question is caused by the fact that the first example in practice still does not work thread-safe, although this is the only method of correcting a public object. I'm figuring out why.
Thanks for the reply.
public class B {
public synchronized void updateA(A a) {
....
}
}
public class B {
public void updateA(A a) {
synchronized (this) {
....
}
}
}
public class B {
public void updateA(A a) {
synchronized (a) {
....
}
}
}