Simple Java name based locks?

匿名 (未验证) 提交于 2019-12-03 02:00:02

问题:

MySQL has a handy function:

SELECT GET_LOCK("SomeName") 

This can be used to create simple, but very specific, name based locks for an application. However, it requires a database connection.

I have many situations like:

someMethod() {     // do stuff to user A for their data for feature X } 

It doesn't make sense to simply synchronize this method, because, for example, if this method is called for user B in the meantime, user B does not need to wait for user A to finish before it starts, only operations for the user A and feature X combination need to wait.

With the MySql lock I could do something like:

someMethod() {     executeQuery("SELECT GET_LOCK('userA-featureX')")     // only locked for user A for their data for feature X     executeQuery("SELECT RELEASE_LOCK('userA-featureX')") } 

Since Java locking is based on objects, it seems like I would need to create a new object to represent the situation for this lock and then put it in a static cache somewhere so all the threads can see it. Subsequent requests to lock for that situation would then locate the lock object in the cache and acquire its lock. I tried to create something like this, but then the lock cache itself needs synchronization. Also, it is difficult to detect when a lock object is no longer being used so that it can be removed from the cache.

I have looked at the Java concurrent packages, but nothing stands out as being able to handle something like this. Is there an easy way to implement this, or am I looking at this from the wrong perspective?

Edit:

To clarify, I am not looking to create a predefined pool of locks ahead of time, I would like to create them on demand. Some pseudo code for what I am thinking is:

LockManager.acquireLock(String name) {     Lock lock;        synchronized (map) {         lock = map.get(name);          // doesn't exist yet - create and store         if(lock == null) {             lock = new Lock();             map.put(name, lock);         }     }      lock.lock(); }  LockManager.releaseLock(String name) {     // unlock     // if this was the last hold on the lock, remove it from the cache } 

回答1:

maybe this is useful for you: jkeylockmanager

Edit:

My initial response was probably a bit short. I am the author and was faced with this problem several times and could not find an existing solution. That's why I made this small library on Google Code.



回答2:

All those answers I see are way too complicated. Why not simply use:

public void executeInNamedLock(String lockName, Runnable runnable) {   synchronized(lockName.intern()) {     runnable.run();   } } 

The key point is the method intern: it ensures that the String returned is a global unique object, and so it can be used as a vm-instance-wide mutex. All interned Strings are held in a global pool, so that's your static cache you were talking about in your original question. Don't worry about memleaks; those strings will be gc'ed if no other thread references it. Note however, that up to and including Java6 this pool is kept in PermGen space instead of the heap, so you might have to increase it.

There's a problem though if some other code in your vm locks on the same string for completely different reasons, but a) this is very unlikely, and b) you can get around it by introducing namespaces, e.g. executeInNamedLock(this.getClass().getName() + "_" + myLockName);



回答3:

Can you have a Map? Each time you require a lock, you basically call map.get(lockName).lock().

Here's an example using Google Guava:

Map lockMap = new MapMaker().makeComputingMap(new Function() {   @Override public Lock apply(String input) {     return new ReentrantLock();   } }); 

Then lockMap.get("anyOldString") will cause a new lock to be created if required and returned to you. You can then call lock() on that lock. makeComputingMap returns a Map that is thread-safe, so you can just share that with all your threads.



回答4:

// pool of names that are being locked HashSet pool = new HashSet();   lock(name)     synchronized(pool)         while(pool.contains(name)) // already being locked             pool.wait();           // wait for release         pool.add(name);            // I lock it  unlock(name)     synchronized(pool)         pool.remove(name);         pool.notifyAll(); 


回答5:

For locking on something like a user name, in-memory Locks in a map might be a bit leaky. As an alternative, you could look at using WeakReferences with WeakHashMap to create mutex objects that can be garbage collected when nothing refers to them. This avoids you having to do any manual reference counting to free up memory.

You can find an implementation here. Note that if you're doing frequent lookups on the map you may run into contention issues acquiring the mutex.



回答6:

Maybe a little later but you can use Google Guava Striped

Conceptually, lock striping is the technique of dividing a lock into many stripes, increasing the granularity of a single lock and allowing independent operations to lock different stripes and proceed concurrently, instead of creating contention for a single lock.

//init stripes=Striped.lazyWeakLock(size); //or stripes=Striped.lock(size); //... Lock lock=stripes.get(object); 


回答7:

A generic solution using java.util.concurrent

import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.ReentrantLock;  public class LockByName {      ConcurrentHashMap mapStringLock;      public LockByName(){         mapStringLock = new ConcurrentHashMap();     }      public LockByName(ConcurrentHashMap mapStringLock){         this.mapStringLock = mapStringLock;     }      @SuppressWarnings("unchecked")     public L getLock(String key) {         L initValue = (L) createIntanceLock();         L lock = mapStringLock.putIfAbsent(key, initValue);         if (lock == null) {             lock = initValue;         }         return lock;     }      protected Object createIntanceLock() {         return new ReentrantLock();     }      public static void main(String[] args) {          LockByName reentrantLocker = new LockByName();          ReentrantLock reentrantLock1 = reentrantLocker.getLock("pepe");          try {             reentrantLock1.lock();             //DO WORK          }finally{             reentrantLock1.unlock();          }       }  } 


回答8:

Based on the answer of McDowell and his class IdMutexProvider, I have written the generic class LockMap that uses WeakHashMap to store lock objects. LockMap.get() can be used to retrieve a lock object for a key, which can then be used with the Java synchronized (...) statement to apply a lock. Unused lock objects are automatically freed during garbage collection.

import java.lang.ref.WeakReference; import java.util.WeakHashMap;  // A map that creates and stores lock objects for arbitrary keys values. // Lock objects which are no longer referenced are automatically released during garbage collection. // Author: Christian d'Heureuse, www.source-code.biz // Based on IdMutexProvider by McDowell, http://illegalargumentexception.blogspot.ch/2008/04/java-synchronizing-on-transient-id.html // See also https://stackoverflow.com/questions/5639870/simple-java-name-based-locks public class LockMap {  private WeakHashMap,WeakReference>> map;  public LockMap() {    map = new WeakHashMap,WeakReference>>(); }  // Returns a lock object for the specified key. public synchronized Object get (KEY key) {    if (key == null) {       throw new NullPointerException(); }    KeyWrapper newKeyWrapper = new KeyWrapper(key);    WeakReference> ref = map.get(newKeyWrapper);    KeyWrapper oldKeyWrapper = (ref == null) ? null : ref.get();    if (oldKeyWrapper != null) {       return oldKeyWrapper; }    map.put(newKeyWrapper, new WeakReference>(newKeyWrapper));    return newKeyWrapper; }  // Returns the number of used entries in the map. public synchronized int size() {    return map.size(); }  // KeyWrapper wraps a key value and is used in three ways: // - as the key for the internal WeakHashMap // - as the value for the internal WeakHashMap, additionally wrapped in a WeakReference // - as the lock object associated to the key private static class KeyWrapper {    private KEY key;    private int hashCode;    public KeyWrapper (KEY key) {       this.key = key;       hashCode = key.hashCode(); }    public boolean equals (Object obj) {       if (obj == this) {          return true; }       if (obj instanceof KeyWrapper) {          return ((KeyWrapper)obj).key.equals(key); }       return false; }    public int hashCode() {       return hashCode; }}  } // end class LockMap 

Example of how to use the LockMap class:

private static LockMap lockMap = new LockMap();  synchronized (lockMap.get(name)) {    ...  } 

A simple test program for the LockMap class:

public static Object lock1; public static Object lock2;  public static void main (String[] args) throws Exception {    System.out.println("TestLockMap Started");    LockMap map = new LockMap();    lock1 = map.get(1);    lock2 = map.get(2);    if (lock2 == lock1) {       throw new Error(); }    Object lock1b = map.get(1);    if (lock1b != lock1) {       throw new Error(); }    if (map.size() != 2) {       throw new Error(); }    for (int i=0; i

If anyone knows a better way to automatically test the LockMap class, please write a comment.



回答9:

I'd like to notice that ConcurrentHashMap has built-in locking facility that is enough for simple exclusive multithread lock. No additional Lock objects needed.

Here is an example of such lock map used to enforce at most one active jms processing for single client.

private static final ConcurrentMap lockMap = new ConcurrentHashMap(); private static final Object DUMMY = new Object();  private boolean tryLock(String key) {     if (lockMap.putIfAbsent(key, DUMMY) != null) {         return false;     }     try {         if (/* attempt cluster-wide db lock via select for update nowait */) {             return true;         } else {             unlock(key);             log.debug("DB is already locked");             return false;         }     } catch (Throwable e) {         unlock(key);         log.debug("DB lock failed", e);         return false;     } }  private void unlock(String key) {     lockMap.remove(key); }  @TransactionAttribute(TransactionAttributeType.REQUIRED) public void onMessage(Message message) {     String key = getClientKey(message);     if (tryLock(key)) {         try {             // handle jms         } finally {             unlock(key);         }     } else {         // key is locked, forcing redelivery         messageDrivenContext.setRollbackOnly();     } } 


回答10:

2 years later but I was looking for a simple named locker solution and came across this, was usefull but I needed a simpler answer, so below what I came up with.

Simple lock under some name and release again under that same name.

private void doTask(){   locker.acquireLock(name);   try{     //do stuff locked under the name   }finally{     locker.releaseLock(name);   } } 

Here is the code:

public class NamedLocker {     private ConcurrentMap synchSemaphores = new ConcurrentHashMap();     private int permits = 1;      public NamedLocker(){         this(1);     }      public NamedLocker(int permits){         this.permits = permits;     }      public void acquireLock(String... key){         Semaphore tempS = new Semaphore(permits, true);         Semaphore s = synchSemaphores.putIfAbsent(Arrays.toString(key), tempS);         if(s == null){             s = tempS;         }         s.acquireUninterruptibly();     }      public void releaseLock(String... key){         Semaphore s = synchSemaphores.get(Arrays.toString(key));         if(s != null){             s.release();         }     } } 


回答11:

Maybe something like that:

public class ReentrantNamedLock {  private class RefCounterLock {      public int counter;     public ReentrantLock sem;      public RefCounterLock() {         counter = 0;         sem = new ReentrantLock();     } } private final ReentrantLock _lock = new ReentrantLock(); private final HashMap _cache = new HashMap();  public void lock(String key) {     _lock.lock();     RefCounterLock cur = null;     try {         if (!_cache.containsKey(key)) {             cur = new RefCounterLock();             _cache.put(key, cur);         } else {             cur = _cache.get(key);         }         cur.counter++;     } finally {         _lock.unlock();     }     cur.sem.lock(); }  public void unlock(String key) {     _lock.lock();     try {         if (_cache.containsKey(key)) {             RefCounterLock cur = _cache.get(key);             cur.counter--;             cur.sem.unlock();             if (cur.counter == 0) { //last reference                 _cache.remove(key);             }             cur = null;         }     } finally {         _lock.unlock();     } }} 

I didn't test it though.



回答12:

After some disappointment that there is no language level support or simple Guava/Commons class for named locks,

This is what I settled down to:

ConcurrentMap locks = new ConcurrentHashMap();  Object getLock(String name) {     Object lock = locks.get(name);     if (lock == null) {         Object newLock = new Object();         lock = locks.putIfAbsent(name, newLock);         if (lock == null) {             lock = newLock;         }     }     return lock; }  void somethingThatNeedsNamedLocks(String name) {     synchronized(getLock(name)) {         // some operations mutually exclusive per each name     } } 

Here I achieved: little boilerplate code with no library dependency, atomically acquiring the lock object, not polluting the global interned string objects, no low-level notify/wait chaos and no try-catch-finally mess.



回答13:

Similar to the answer from Lyomi, but uses the more flexible ReentrantLock instead of a synchronized block.

public class NamedLock {     private static final ConcurrentMap lockByName = new ConcurrentHashMap();      public static void lock(String key)     {         Lock lock = new ReentrantLock();         Lock existingLock = lockByName.putIfAbsent(key, lock);          if(existingLock != null)         {             lock = existingLock;         }         lock.lock();     }      public static void unlock(String key)      {         Lock namedLock = lockByName.get(key);         namedLock.unlock();     } } 

Yes this will grow over time - but using the ReentrantLock opens up greater possibilities for removing the lock from the map. Although, removing items from the map doesn't seem all that useful considering removing values from the map will not shrink its size. Some manual map sizing logic would have to be implemented.



回答14:

Memory consideration

Often times, synchronization needed for a particular key is short-lived. Keeping around released keys can lead to excessive memory waste, making it non-viable.

Here's an implementation does not internally keep around released keys.

import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.CountDownLatch;  public class KeyedMutexes {      private final ConcurrentMap key2Mutex = new ConcurrentHashMap();      public void lock(K key) throws InterruptedException {         final CountDownLatch ourLock = new CountDownLatch(1);         for (;;) {             CountDownLatch theirLock = key2Mutex.putIfAbsent(key, ourLock);             if (theirLock == null) {                 return;             }             theirLock.await();         }     }      public void unlock(K key) {         key2Mutex.remove(key).countDown();     } } 

Reentrancy, and other bells and whistles

If one wants re-entrant lock semantics, they can extend the above by wrapping the mutex object in a class that keeps track of the locking thread and locked count.

e.g.:

private static class Lock {     final CountDownLatch mutex = new CountDownLatch(1);      final long threadId = Thread.currentThread().getId();      int lockedCount = 1; } 

If one wants lock() to return an object to make releases easier and safer, that's also a possibility.

Putting it all together, here's what the class could look like:

public class KeyedReentrantLocks {      private final ConcurrentMap key2Lock = new ConcurrentHashMap();      public KeyedLock acquire(K key) throws InterruptedException {         final KeyedLock ourLock = new KeyedLock() {             @Override             public void close() {                 if (Thread.currentThread().getId() != threadId) {                     throw new IllegalStateException("wrong thread");                 }                 if (--lockedCount == 0) {                     key2Lock.remove(key);                     mutex.countDown();                 }             }         };         for (;;) {             KeyedLock theirLock = key2Lock.putIfAbsent(key, ourLock);             if (theirLock == null) {                 return ourLock;             }             if (theirLock.threadId == Thread.currentThread().getId()) {                 theirLock.lockedCount++;                 return theirLock;             }             theirLock.mutex.await();         }     }      public static abstract class KeyedLock implements AutoCloseable {         protected final CountDownLatch mutex = new CountDownLatch(1);         protected final long threadId = Thread.currentThread().getId();         protected int lockedCount = 1;          @Override         public abstract void close();     } } 

And here's how one might use it:

try (KeyedLock lock = locks.acquire("SomeName")) {      // do something critical here } 


回答15:

In response to the suggestion of using new MapMaker().makeComputingMap()...

MapMaker().makeComputingMap() is deprecated for safety reasons. The successor is CacheBuilder. With weak keys/values applied to CacheBuilder, we're soooo close to a solution.

The problem is a note in CacheBuilder.weakKeys():

when this method is used, the resulting cache will use identity (==) comparison to determine equality of keys.  

This makes it impossible to select an existing lock by string value. Erg.



回答16:

(4 years later...) My answer is similar to user2878608's but I think there are some missing edge cases in that logic. I also thought Semaphore was for locking multiple resources at once (though I suppose using it for counting lockers like that is fine too), so I used a generic POJO lock object instead. I ran one test on it which demonstrated each of the edge cases existed IMO and will be using it on my project at work. Hope it helps someone. :)

class Lock {     int c;  // count threads that require this lock so you don't release and acquire needlessly }  ConcurrentHashMap map = new ConcurrentHashMap();  LockManager.acquireLock(String name) {     Lock lock = new Lock();  // creating a new one pre-emptively or checking for null first depends on which scenario is more common in your use case     lock.c = 0;      while( true )     {         Lock prevLock = map.putIfAbsent(name, lock);         if( prevLock != null )             lock = prevLock;          synchronized (lock)         {             Lock newLock = map.get(name);             if( newLock == null )                 continue;  // handles the edge case where the lock got removed while someone was still waiting on it             if( lock != newLock )             {                 lock = newLock;  // re-use the latest lock                 continue;  // handles the edge case where a new lock was acquired and the critical section was entered immediately after releasing the lock but before the current locker entered the sync block             }              // if we already have a lock             if( lock.c > 0 )             {                 // increase the count of threads that need an offline director lock                 ++lock.c;                 return true;  // success             }             else             {                 // safely acquire lock for user                 try                 {                     perNameLockCollection.add(name);  // could be a ConcurrentHashMap or other synchronized set, or even an external global cluster lock                     // success                     lock.c = 1;                     return true;                 }                 catch( Exception e )                 {                     // failed to acquire                     lock.c = 0;  // this must be set in case any concurrent threads are waiting                     map.remove(name);  // NOTE: this must be the last critical thing that happens in the sync block!                 }             }         }     } }  LockManager.releaseLock(String name) {     // unlock     // if this was the last hold on the lock, remove it from the cache      Lock lock = null;  // creating a new one pre-emptively or checking for null first depends on which scenario is more common in your use case      while( true )     {         lock = map.get(name);         if( lock == null )         {             // SHOULD never happen             log.Error("found missing lock! perhaps a releaseLock call without corresponding acquireLock call?! name:"+name);             lock = new Lock();             lock.c = 1;             Lock prevLock = map.putIfAbsent(name, lock);             if( prevLock != null )                 lock = prevLock;         }          synchronized (lock)         {             Lock newLock = map.get(name);             if( newLock == null )                 continue;  // handles the edge case where the lock got removed while someone was still waiting on it             if( lock != newLock )             {                 lock = newLock;  // re-use the latest lock                 continue;  // handles the edge case where a new lock was acquired and the critical section was entered immediately after releasing the lock but before the current locker entered the sync block             }              // if we are not the last locker             if( lock.c > 1 )             {                 // decrease the count of threads that need an offline director lock                 --lock.c;                 return true;  // success             }             else             {                 // safely release lock for user                 try                 {                     perNameLockCollection.remove(name);  // could be a ConcurrentHashMap or other synchronized set, or even an external global cluster lock                     // success                     lock.c = 0;  // this must be set in case any concurrent threads are waiting                     map.remove(name);  // NOTE: this must be the last critical thing that happens in the sync block!                     return true;                 }                 catch( Exception e )                 {                     // failed to release                     log.Error("unable to release lock! name:"+name);                     lock.c = 1;                     return false;                 }             }         }     }  } 


回答17:

I've created a tokenProvider based on the IdMutexProvider of McDowell. The manager uses a WeakHashMap which takes care of cleaning up unused locks.

TokenManager:

/**  * Token provider used to get a {@link Mutex} object which is used to get exclusive access to a given TOKEN.  * Because WeakHashMap is internally used, Mutex administration is automatically cleaned up when  * the Mutex is no longer is use by any thread.  *  * 
  * Usage:  * private final TokenMutexProvider<String> myTokenProvider = new TokenMutexProvider<String>();  *  * Mutex mutex = myTokenProvider.getMutex("123456");  * synchronized (mutex) {  *  // your code here  * }  * 
* * Class inspired by McDowell. * url: http://illegalargumentexception.blogspot.nl/2008/04/java-synchronizing-on-… * * @param type of token. It is important that the equals method of that Object return true * for objects of different instances but with the same 'identity'. (see {@link WeakHashMap}).
* E.g. *
  *  String key1 = "1";  *  String key1b = new String("1");  *  key1.equals(key1b) == true;  *  *  or  *  Integer key1 = 1;  *  Integer key1b = new Integer(1);  *  key1.equals(key1b) == true;  * 
*/ public class TokenMutexProvider { private final Map> mutexMap = new WeakHashMap>(); /** * Get a {@link Mutex} for the given (non-null) token. */ public Mutex getMutex(TOKEN token) { if (token==null) { throw new NullPointerException(); } Mutex key = new MutexImpl(token); synchronized (mutexMap) { WeakReference ref = mutexMap.get(key); if (ref==null) { mutexMap.put(key, new WeakReference(key)); return key; } Mutex mutex = ref.get(); if (mutex==null) { mutexMap.put(key, new WeakReference(key)); return key; } return mutex; } } public int size() { synchronized (mutexMap) { return mutexMap.size(); } } /** * Mutex for acquiring exclusive access to a token. */ public static interface Mutex {} private class MutexImpl implements Mutex { private final TOKEN token; protected MutexImpl(TOKEN token) { this.token = token; } @Override public boolean equals(Object other) { if (other==null) { return false; } if (getClass()==other.getClass()) { TOKEN otherToken = ((MutexImpl)other).token; return token.equals(otherToken); } return false; } @Override public int hashCode() { return token.hashCode(); } } }

Usage:

private final TokenMutexManager myTokenManager = new TokenMutexManager();  Mutex mutex = myTokenManager.getMutex("UUID_123456"); synchronized(mutex) {     // your code here } 

or rather use Integers?

private final TokenMutexManager myTokenManager = new TokenMutexManager();  Mutex mutex = myTokenManager.getMutex(123456); synchronized(mutex) {     // your code here } 


回答18:

This thread is old, but a possible solution is the framework https://github.com/brandaof/named-lock.

NamedLockFactory lockFactory = new NamedLockFactory();  ...  Lock lock = lockFactory.getLock("lock_name"); lock.lock();  try{   //manipulate protected state } finally{     lock.unlock(); } 


回答19:

Your idea about a shared static repository of lock objects for each situation is correct.
You don't need the cache itself to be synchronized ... it can be as simple as a hash map.

Threads can simultaneously get a lock object from the map. The actual synchronization logic should be encapsulated within each such object separately (see the java.util.concurrent package for that - http://download.oracle.com/javase/6/docs/api/java/util/concurrent/locks/package-summary.html)



回答20:

TreeMap because in HashMap size of inner array can only increase

public class Locker {     private final Object lock = new Object();     private final Map map = new TreeMap();      public Value lock(T id) {         Value r;         synchronized (lock) {             if (!map.containsKey(id)) {                 Value value = new Value();                 value.id = id;                 value.count = 0;                 value.lock = new ReentrantLock();                 map.put(id, value);             }             r = map.get(id);             r.count++;         }         r.lock.lock();         return r;     }      public void unlock(Value r) {         r.lock.unlock();         synchronized (lock) {             r.count--;             if (r.count == 0)                 map.remove(r.id);         }     }      public static class Value {          private Lock lock;         private long count;         private T id;     } } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!