com.helger.commons.concurrent.SimpleReadWriteLock Maven / Gradle / Ivy
/**
* Copyright (C) 2014-2015 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.helger.commons.concurrent;
import java.io.Serializable;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.helger.commons.ValueEnforcer;
import com.helger.commons.callback.INonThrowingCallable;
import com.helger.commons.callback.IThrowingCallable;
import com.helger.commons.callback.IThrowingRunnable;
/**
* This is an extension around {@link ReentrantReadWriteLock} that allows for
* easy usage with Java 8 :) See {@link #readLocked(Runnable)} and
* {@link #writeLocked(Runnable)} method. For throwing versions see
* {@link #readLockedThrowing(IThrowingRunnable)} and
* {@link #writeLockedThrowing(IThrowingRunnable)}. Also methods for callables
* are available.
*
* @author Philip Helger
*/
public class SimpleReadWriteLock implements ReadWriteLock, Serializable
{
private final ReentrantReadWriteLock m_aRWLock;
/**
* Default constructor creating a default {@link ReentrantReadWriteLock}
*/
public SimpleReadWriteLock ()
{
m_aRWLock = new ReentrantReadWriteLock ();
}
/**
* Constructor creating a {@link ReentrantReadWriteLock} with the provided
* fairness
*
* @param bFair
* true
if this lock should use a fair ordering policy
*/
public SimpleReadWriteLock (final boolean bFair)
{
m_aRWLock = new ReentrantReadWriteLock (bFair);
}
@Nonnull
public Lock readLock ()
{
return m_aRWLock.readLock ();
}
@Nonnull
public Lock writeLock ()
{
return m_aRWLock.writeLock ();
}
/**
* Execute the provided runnable in a read lock.
*
* @param aRunnable
* Runnable to be executed. May not be null
.
*/
public void readLocked (@Nonnull final Runnable aRunnable)
{
ValueEnforcer.notNull (aRunnable, "Runnable");
m_aRWLock.readLock ().lock ();
try
{
aRunnable.run ();
}
finally
{
m_aRWLock.readLock ().unlock ();
}
}
/**
* Execute the provided runnable in a read lock.
*
* @param aRunnable
* Runnable to be executed. May not be null
.
* @throws EXTYPE
* If the callable throws the exception
* @param
* Exception type to be thrown
*/
public void readLockedThrowing (@Nonnull final IThrowingRunnable aRunnable) throws EXTYPE
{
ValueEnforcer.notNull (aRunnable, "Runnable");
m_aRWLock.readLock ().lock ();
try
{
aRunnable.run ();
}
finally
{
m_aRWLock.readLock ().unlock ();
}
}
/**
* Execute the provided callable in a read lock.
*
* @param aCallable
* Callable to be executed. May not be null
.
* @return The return value of the callable. May be null
.
* @param
* Return type
*/
@Nullable
public T readLocked (@Nonnull final INonThrowingCallable aCallable)
{
ValueEnforcer.notNull (aCallable, "Callable");
m_aRWLock.readLock ().lock ();
try
{
return aCallable.call ();
}
finally
{
m_aRWLock.readLock ().unlock ();
}
}
/**
* Execute the provided callable in a read lock.
*
* @param aCallable
* Callable to be executed. May not be null
.
* @return The return value of the callable. May be null
.
* @throws EXTYPE
* If the callable throws the exception
* @param
* Return type
* @param
* Exception type to be thrown
*/
@Nullable
public T readLockedThrowing (@Nonnull final IThrowingCallable aCallable) throws EXTYPE
{
ValueEnforcer.notNull (aCallable, "Callable");
m_aRWLock.readLock ().lock ();
try
{
return aCallable.call ();
}
finally
{
m_aRWLock.readLock ().unlock ();
}
}
/**
* Execute the provided runnable in a write lock.
*
* @param aRunnable
* Runnable to be executed. May not be null
.
*/
public void writeLocked (@Nonnull final Runnable aRunnable)
{
ValueEnforcer.notNull (aRunnable, "Runnable");
m_aRWLock.writeLock ().lock ();
try
{
aRunnable.run ();
}
finally
{
m_aRWLock.writeLock ().unlock ();
}
}
/**
* Execute the provided runnable in a write lock.
*
* @param aRunnable
* Runnable to be executed. May not be null
.
* @throws EXTYPE
* If the runnable throws the exception
* @param
* Exception type to be thrown
*/
public void writeLockedThrowing (@Nonnull final IThrowingRunnable aRunnable) throws EXTYPE
{
ValueEnforcer.notNull (aRunnable, "Runnable");
m_aRWLock.writeLock ().lock ();
try
{
aRunnable.run ();
}
finally
{
m_aRWLock.writeLock ().unlock ();
}
}
/**
* Execute the provided callable in a write lock.
*
* @param aCallable
* Callable to be executed. May not be null
.
* @return The return value of the callable. May be null
.
* @param
* Return type
*/
@Nullable
public T writeLocked (@Nonnull final INonThrowingCallable aCallable)
{
ValueEnforcer.notNull (aCallable, "Callable");
m_aRWLock.writeLock ().lock ();
try
{
return aCallable.call ();
}
finally
{
m_aRWLock.writeLock ().unlock ();
}
}
/**
* Execute the provided callable in a write lock.
*
* @param aCallable
* Callable to be executed. May not be null
.
* @return The return value of the callable. May be null
.
* @throws EXTYPE
* If the callable throws the exception
* @param
* Return type
* @param
* Exception type to be thrown
*/
@Nullable
public T writeLockedThrowing (@Nonnull final IThrowingCallable aCallable) throws EXTYPE
{
ValueEnforcer.notNull (aCallable, "Callable");
m_aRWLock.writeLock ().lock ();
try
{
return aCallable.call ();
}
finally
{
m_aRWLock.writeLock ().unlock ();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy