I needed a set of distributed locks for GaeVFS<http://code.google.com/p/gaevfs/> that work across multiple JVM instances, and chose to implement them to the Lock<http://java.sun.com/javase/6/docs/api/java/util/concurrent/locks/Lock.html>and ReadWriteLock<http://java.sun.com/javase/6/docs/api/java/util/concurrent/locks/ReadWriteLock.html>interfaces of the java.util.concurrent.locks<http://java.sun.com/javase/6/docs/api/java/util/concurrent/locks/package-summary.html>package in the hope that they might be generally useful for other applications. The source code<http://code.google.com/p/gaevfs/source/browse/trunk/src/com/newatlanta/appengine/locks/>for the initial implementation of these locks is available from the GaeVFS project web site in the com.newatlanta.appengine.locks<http://code.google.com/p/gaevfs/source/browse/trunk/src/com/newatlanta/appengine/locks/>package (junit tests are in the com.newatlanta.appengine.junit.locks<http://code.google.com/p/gaevfs/source/browse/trunk/test/src/com/newatlanta/appengine/junit/locks/>package), which includes the following classes:
ExclusiveLock Implements a mutual exclusion lock with the following features: - re-entrant locking by the thread that owns the lock (matching unlocks are required to fully release the lock) - only the thread that owns the lock can unlock it - locks automatically expire after 30 seconds (the GAE request timeout limit) - locks can be acquired with a single call to memcache ReadWriteLock Implements a "many readers, one writer" scheme with the following features: - the write lock is mutually exclusive and can only be acquired if no other threads are holding the read lock - the read lock is shared and can only be acquired if no other threads are holding the write lock - the write lock is re-entrant by the thread that owns the lock (matching unlocks are required to fully release the lock) - the thread that owns the write lock may also acquire the read lock (it's not necessary to unlock the read lock--releasing the write lock also releases the read lock) - write locks automatically expire after 30 seconds (the GAE request timeout limit) Using the ReadWriteLock can be fairly expensive; it takes a minimum of two memcache calls to acquire the write lock, and a minimum of three memcache calls to acquire a read lock. Therefore, it might be better to use an ExclusiveLock--which can be acquired with a minimum of one memcache call--depending on our application. For example, if your application's read operations are short and fast, then an ExclusiveLock might perform better than a ReadWriteLock. SharedLock Implements a counter-based shared lock that isn't particularly useful by itself, but is used as a base class for the ReadWriteLock implementation. AbstractLock The base class for ExclusiveLock and SharedLock. The implementations of these locks are based on the atomic features of the MemcacheService<http://code.google.com/appengine/docs/java/javadoc/com/google/appengine/api/memcache/package-summary.html>, and are therefore unreliable because memcache can evict them at any time. This shouldn't be too much of a concern for the ExclusiveLock or the "write lock" of the ReadWriteLock because the maximum lifetime of these locks is only 30 seconds (the maximum lifetime of a request). However, this is a concern for the counter-based SharedLock and the "read lock" of the ReadWriteLock. I plan to address this by persisting the counters to the GAE datastore via a write-behind mechanism as soon as Task Queues are available for GAE/J. I haven't done any performance testing of these locks (maybe when profiling is available on GAE/J?). As with any synchronization mechanism, use these sparingly, since misuse can badly harm the performance of your application. Any comments or feedback is welcomed. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google App Engine for Java" group. To post to this group, send email to google-appengine-java@googlegroups.com To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en -~----------~----~----~----~------~----~------~--~---