Skip to content

Instantly share code, notes, and snippets.

@lincolnthree
Created March 14, 2013 17:07
Show Gist options
  • Select an option

  • Save lincolnthree/5163147 to your computer and use it in GitHub Desktop.

Select an option

Save lincolnthree/5163147 to your computer and use it in GitHub Desktop.
@Override
public Lock obtainLock(LockMode mode)
{
ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
if (LockMode.READ.equals(mode))
return readWriteLock.readLock();
else
return readWriteLock.writeLock();
}
@Override
public <T> T performLocked(LockMode mode, Callable<T> task)
{
Assert.notNull(mode, "LockMode must not be null.");
Assert.notNull(task, "Task to perform must not be null.");
Lock lock = obtainLock(mode);
lock.lock();
T result;
try
{
result = task.call();
}
catch (Exception e)
{
throw new ContainerException(e);
}
finally
{
lock.unlock();
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment