Created
December 12, 2011 17:22
-
-
Save simonetripodi/1468252 to your computer and use it in GitHub Desktop.
SynchronizedObjectPool using Read/Write lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static class SynchronizedObjectPool<T> implements ObjectPool<T> { | |
/** Object whose monitor is used to synchronize methods on the wrapped pool. */ | |
private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); | |
/** the underlying object pool */ | |
private final ObjectPool<T> pool; | |
/** | |
* Create a new SynchronizedObjectPool wrapping the given pool. | |
* | |
* @param pool the ObjectPool to be "wrapped" in a synchronized ObjectPool. | |
* @throws IllegalArgumentException if the pool is null | |
*/ | |
SynchronizedObjectPool(final ObjectPool<T> pool) throws IllegalArgumentException { | |
if (pool == null) { | |
throw new IllegalArgumentException("pool must not be null."); | |
} | |
this.pool = pool; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public T borrowObject() throws Exception, NoSuchElementException, IllegalStateException { | |
ReadLock readLock = readWriteLock.readLock(); | |
readLock.lock(); | |
try { | |
return pool.borrowObject(); | |
} finally { | |
readLock.unlock(); | |
} | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public void returnObject(final T obj) { | |
WriteLock writeLock = readWriteLock.writeLock(); | |
writeLock.lock(); | |
try { | |
pool.returnObject(obj); | |
} catch (Exception e) { | |
// swallowed as of Pool 2 | |
} finally { | |
writeLock.unlock(); | |
} | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public void invalidateObject(final T obj) { | |
WriteLock writeLock = readWriteLock.writeLock(); | |
writeLock.lock(); | |
try { | |
pool.invalidateObject(obj); | |
} catch (Exception e) { | |
// swallowed as of Pool 2 | |
} finally { | |
writeLock.unlock(); | |
} | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public void addObject() throws Exception, IllegalStateException, UnsupportedOperationException { | |
WriteLock writeLock = readWriteLock.writeLock(); | |
writeLock.lock(); | |
try { | |
pool.addObject(); | |
} finally { | |
writeLock.unlock(); | |
} | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public int getNumIdle() throws UnsupportedOperationException { | |
ReadLock readLock = readWriteLock.readLock(); | |
readLock.lock(); | |
try { | |
return pool.getNumIdle(); | |
} finally { | |
readLock.unlock(); | |
} | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public int getNumActive() throws UnsupportedOperationException { | |
ReadLock readLock = readWriteLock.readLock(); | |
readLock.lock(); | |
try { | |
return pool.getNumActive(); | |
} finally { | |
readLock.unlock(); | |
} | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public void clear() throws Exception, UnsupportedOperationException { | |
WriteLock writeLock = readWriteLock.writeLock(); | |
writeLock.lock(); | |
try { | |
pool.clear(); | |
} finally { | |
writeLock.unlock(); | |
} | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public void close() { | |
WriteLock writeLock = readWriteLock.writeLock(); | |
writeLock.lock(); | |
try { | |
pool.close(); | |
} catch (Exception e) { | |
// swallowed as of Pool 2 | |
} finally { | |
writeLock.unlock(); | |
} | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public String toString() { | |
final StringBuffer sb = new StringBuffer(); | |
sb.append("SynchronizedObjectPool"); | |
sb.append("{pool=").append(pool); | |
sb.append('}'); | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment