(Docs: b) http://developer.android.com/reference/java/util/concurrent/locks/Lock.html a) http://developer.android.com/reference/java/util/concurrent/locks/ReentrantLock.html
)
A lock is a tool for controlling access to a shared resource by
multiple threads. Commonly, a lock provides exclusive access to a
shared resource: only one thread at a time can acquire the lock and
all access to the shared resource requires that the lock be
acquired first.
Sample code:
Lock l = new ReentrantLock();
l.lock();
try
{
// access the resource protected by this lock
}
finally
{
l.unlock();
}
(Docs:
a) http://developer.android.com/reference/java/util/concurrent/locks/ReadWriteLock.html
b) http://developer.android.com/reference/java/util/concurrent/locks/ReentrantReadWriteLock.html
)
A ReadWriteLock maintains a pair of associated locks, one for read-only operations and one for writing. The read lock may be held simultaneously by multiple reader threads, so long as there are no writers. The write lock is exclusive. That means only a single thread at a time (a writer thread) can modify the shared data, in many cases any
number of threads can concurrently read the data (hence reader threads).
Reentrancy also allows downgrading from the write lock to a read lock,
by acquiring the write lock, then the read lock and then releasing the
write lock. However, upgrading from a read lock to the write lock is not possible.
Please refer http://developer.android.com/reference/java/util/concurrent/locks/ReentrantReadWriteLock.html to get sample.