com.helger.commons.concurrent.SimpleLock 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.util.concurrent.locks.ReentrantLock;
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 ReentrantLock} that allows for easy usage
* with Java 8 :) See {@link #locked(Runnable)} and {@link #locked(Runnable)}
* method. Also methods for suppliers are available.
*
* @author Philip Helger
*/
public class SimpleLock extends ReentrantLock
{
/**
* Default constructor creating a default {@link ReentrantReadWriteLock}
*/
public SimpleLock ()
{}
/**
* Constructor creating a {@link ReentrantReadWriteLock} with the provided
* fairness
*
* @param bFair
* true
if this lock should use a fair ordering policy
*/
public SimpleLock (final boolean bFair)
{
super (bFair);
}
/**
* Execute the provided runnable in a read lock.
*
* @param aRunnable
* Runnable to be executed. May not be null
.
*/
public void locked (@Nonnull final Runnable aRunnable)
{
ValueEnforcer.notNull (aRunnable, "Runnable");
lock ();
try
{
aRunnable.run ();
}
finally
{
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 lockedThrowing (@Nonnull final IThrowingRunnable aRunnable) throws EXTYPE
{
ValueEnforcer.notNull (aRunnable, "Runnable");
lock ();
try
{
aRunnable.run ();
}
finally
{
unlock ();
}
}
/**
* Execute the provided callable in a read lock.
*
* @param aSupplier
* Callable to be executed. May not be null
.
* @return The return value of the callable. May be null
.
* @param
* Return type
*/
@Nullable
public T locked (@Nonnull final INonThrowingCallable aSupplier)
{
ValueEnforcer.notNull (aSupplier, "Supplier");
lock ();
try
{
return aSupplier.call ();
}
finally
{
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 lockedThrowing (@Nonnull final IThrowingCallable aCallable) throws EXTYPE
{
ValueEnforcer.notNull (aCallable, "Callable");
lock ();
try
{
return aCallable.call ();
}
finally
{
unlock ();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy