A RWLock is extremely useful in scenarios where there are lots more readers and very few writers to a data structure. Also if the read operation by the reader thread could take significant amount of time (binary search etc.)
The usage of Lock can be see as under:
public class MyBTree { private RWLock lock = new Lock(); ..... ..... public Object find(Object o) { try { lock.acquireReadLock(); ....perform complex search to get the Object ... return result; } finally { lock.releaseReadLock(); } } public void insert(Object o) { try { lock.acquireWriteLock(); ....perform complex operation to insert object ... } finally { lock.releaseWriteLock(); } } }
@author Dhiru Pandey 8/7/2000
|
|