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

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -