As suggested in several answers to this question:
What is this blocking technology called?
I implemented ReentrantReadWriteLock and saw great speed (I knew there was some lock in one class, and using the reentrant lock helped speed up the process).
But now I'm wondering: if inside the class all access (both read and write) is done by first locking the read lock or write lock, does this mean that the synchronized keyword should not be used anymore in this class?
For example, here is one official Java 1.6 example found at http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html
class RWDictionary { private final Map<String, Data> m = new TreeMap<String, Data>(); private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); private final Lock r = rwl.readLock(); private final Lock w = rwl.writeLock(); public Data get(String key) { r.lock(); try { return m.get(key); } finally { r.unlock(); } } public String[] allKeys() { r.lock(); try { return m.keySet().toArray(); } finally { r.unlock(); } } public Data put(String key, Data value) { w.lock(); try { return m.put(key, value); } finally { w.unlock(); } } public void clear() { w.lock(); try { m.clear(); } finally { w.unlock(); } } }
There is no synchronize keyword.
Now I understand that one of the points of such locks should be faster than other methods (in this case, faster than synchronization), but what is the technical explanation for this?
Does the read / write lock in the class in each get / update method "replace" the synchronization keyword for these methods?
Cedric martin
source share