java - How to acquire a lock by a key -
what best way prevent concurrent update of 1 record in key-value set without locking entire set? semantically, i'm looking kind of locking key (ideally, java implementation, not necessarily):
interface lockbykey { void lock(string key); // acquire exclusive lock key void unlock(string key); // release lock key }
this lock intended synchronize access remote store, synchronized java collection not option.
guava has being released in 13.0; can out of head if like.
striped<lock>
more or less allocates specific number of locks, , assigns strings locks based on hash code. api looks more or less like
striped<lock> locks = striped.lock(stripes); lock l = locks.get(string); l.lock(); try { // stuff } { l.unlock(); }
more or less, controllable number of stripes lets trade concurrency against memory usage, because allocating full lock each string key can expensive; essentially, lock contention when hash collisions, (predictably) rare.
(disclosure: contribute guava.)
Comments
Post a Comment