public class RWLock extends Object
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();
}
}
}
| Constructor and Description |
|---|
RWLock() |
| Modifier and Type | Method and Description |
|---|---|
void |
acquireReadLock()
This method is used to acquire a read lock.
|
void |
acquireWriteLock()
This method is used to acquire a write lock.
|
boolean |
isWriteLocked()
isWriteLocked
returns true if the RWLock is in a write locked state.
|
void |
releaseReadLock()
This method is used to release a read lock.
|
void |
releaseWriteLock()
This method is used to release a write lock.
|
public void acquireReadLock()
public void acquireWriteLock()
public boolean isWriteLocked()
public void releaseReadLock()
public void releaseWriteLock()
Copyright © 2017. All rights reserved.