Sunday, 8 September 2013

Is synchronized "deep" in java?

Is synchronized "deep" in java?

Lets say you have:
public class Foo {
public Bar b;
}
public class Bar {
public int x;
public int y;
}
And then you call:
Foo f = new Foo();
....
synchronized(f) {
f.b.x = 3;
f.b.y = 5; //x and y must be adjacent primes at all times.
}
If our thread-safeness deep? i.e. does synchronized(f) prevent another
thread from seeing f.b while f.b is updated? What about synchronized(this)
instead?

No comments:

Post a Comment