java - Member method synchronized on whole class -
after quick searching, not able find clear answer.
is there more elegant way synchronize whole fellow member method (bound on instance) on whole class? means method can called on multiple instances multiple threads, 1 thread @ time. can written synchronized block.
(for illustration, method inserts it's instance shared cache.)
class dataobject { public putincache() { synchronized(getclass()) { // ... stuff cache.insert(this); // ... more stuff } } }
it's thought avoid synchronizing on publicly accessible object. can utilize shared monitor object, however:
class dataobject { private static final object cachelock = new object(); public putincache() { synchronized(cachelock) { // ... stuff cache.insert(this); // ... more stuff } } }
note particular implementation dataobject
, classes derived share lock.
java synchronized
No comments:
Post a Comment