java - Does synchronized affect object members? -
if call object synchronized, can access objects inside object if synchronized? or can access data types?
even though goal protect data, synchronization provides exclusivity around block of code, not piece of data. code outside synchronization blocks (or in blocks use different objects), may alter data trying protect if isn't want.
any correct locking strategy must ensure blocks of code interfere each other hold same lock. includes code interfere copy of run in second thread.
synchronized (myobject) { // sensitive code }
locking @ method level shorthand locking this
pointer body of method. (or class
object static method).
Comments
Post a Comment