Concurrency topic:
Would it be possible to change the lazy init to check if write lock is already
locked? It will eliminate the posibility of locking the write lock by more then
one thread.
try {
readLock.lock();
if(is need to init)
{
boolean hasWriteLock = false;
try {
readLock.unlock();
hasWriteLock = writeLock.tryLock(); // Could be locked only
once
if(hasWriteLock)
doInit();
} finally
{
if(hasWriteLock)
writeLock.unlock();
readLock.lock(); // If the write lock is locked read threads will
wait
}
}
return ...
} finally
{
readLock.unlock();
}