Class ConcurrentUtils
java.lang.Object
org.apache.commons.lang3.concurrent.ConcurrentUtils
A utility class providing functionality related to the
java.util.concurrent
package.- Since:
- 3.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Future<T>
constantFuture
(T value) Gets an implementation ofFuture
that is immediately done and returns the specified constant value.static <K,
V> V createIfAbsent
(ConcurrentMap<K, V> map, K key, ConcurrentInitializer<V> init) Checks if a concurrent map contains a key and creates a corresponding value if not.static <K,
V> V createIfAbsentUnchecked
(ConcurrentMap<K, V> map, K key, ConcurrentInitializer<V> init) Checks if a concurrent map contains a key and creates a corresponding value if not, suppressing checked exceptions.static ConcurrentException
Inspects the cause of the specifiedExecutionException
and creates aConcurrentException
with the checked cause if necessary.static ConcurrentRuntimeException
Inspects the cause of the specifiedExecutionException
and creates aConcurrentRuntimeException
with the checked cause if necessary.static void
Handles the specifiedExecutionException
.static void
Handles the specifiedExecutionException
and transforms it into a runtime exception.static <T> T
initialize
(ConcurrentInitializer<T> initializer) Invokes the specifiedConcurrentInitializer
and returns the object produced by the initializer.static <T> T
initializeUnchecked
(ConcurrentInitializer<T> initializer) Invokes the specifiedConcurrentInitializer
and transforms occurring exceptions to runtime exceptions.static <K,
V> V putIfAbsent
(ConcurrentMap<K, V> map, K key, V value) Puts a value in the specifiedConcurrentMap
if the key is not yet present.
-
Method Details
-
constantFuture
Gets an implementation ofFuture
that is immediately done and returns the specified constant value.This can be useful to return a simple constant immediately from the concurrent processing, perhaps as part of avoiding nulls. A constant future can also be useful in testing.
- Type Parameters:
T
- the type of the value used by thisFuture
object- Parameters:
value
- the constant value to return, may be null- Returns:
- an instance of Future that will return the value, never null
-
createIfAbsent
public static <K,V> V createIfAbsent(ConcurrentMap<K, V> map, K key, ConcurrentInitializer<V> init) throws ConcurrentExceptionChecks if a concurrent map contains a key and creates a corresponding value if not. This method first checks the presence of the key in the given map. If it is already contained, its value is returned. Otherwise theget()
method of the passed inConcurrentInitializer
is called. With the resulting objectputIfAbsent(ConcurrentMap, Object, Object)
is called. This handles the case that in the meantime another thread has added the key to the map. Both the map and the initializer can be null; in this case this method simply returns null.- Type Parameters:
K
- the type of the keys of the mapV
- the type of the values of the map- Parameters:
map
- the map to be modifiedkey
- the key of the value to be addedinit
- theConcurrentInitializer
for creating the value- Returns:
- the value stored in the map after this operation; this may or may
not be the object created by the
ConcurrentInitializer
- Throws:
ConcurrentException
- if the initializer throws an exception
-
createIfAbsentUnchecked
public static <K,V> V createIfAbsentUnchecked(ConcurrentMap<K, V> map, K key, ConcurrentInitializer<V> init) Checks if a concurrent map contains a key and creates a corresponding value if not, suppressing checked exceptions. This method callscreateIfAbsent()
. If aConcurrentException
is thrown, it is caught and re-thrown as aConcurrentRuntimeException
.- Type Parameters:
K
- the type of the keys of the mapV
- the type of the values of the map- Parameters:
map
- the map to be modifiedkey
- the key of the value to be addedinit
- theConcurrentInitializer
for creating the value- Returns:
- the value stored in the map after this operation; this may or may
not be the object created by the
ConcurrentInitializer
- Throws:
ConcurrentRuntimeException
- if the initializer throws an exception
-
extractCause
Inspects the cause of the specifiedExecutionException
and creates aConcurrentException
with the checked cause if necessary. This method performs the following checks on the cause of the passed in exception:- If the passed in exception is null or the cause is null, this method returns null.
- If the cause is a runtime exception, it is directly thrown.
- If the cause is an error, it is directly thrown, too.
- In any other case the cause is a checked exception. The method then
creates a
ConcurrentException
, initializes it with the cause, and returns it.
- Parameters:
ex
- the exception to be processed- Returns:
- a
ConcurrentException
with the checked cause
-
extractCauseUnchecked
Inspects the cause of the specifiedExecutionException
and creates aConcurrentRuntimeException
with the checked cause if necessary. This method works exactly likeextractCause(ExecutionException)
. The only difference is that the cause of the specifiedExecutionException
is extracted as a runtime exception. This is an alternative for client code that does not want to deal with checked exceptions.- Parameters:
ex
- the exception to be processed- Returns:
- a
ConcurrentRuntimeException
with the checked cause
-
handleCause
Handles the specifiedExecutionException
. This method callsextractCause(ExecutionException)
for obtaining the cause of the exception - which might already cause an unchecked exception or an error being thrown. If the cause is a checked exception however, it is wrapped in aConcurrentException
, which is thrown. If the passed in exception is null or has no cause, the method simply returns without throwing an exception.- Parameters:
ex
- the exception to be handled- Throws:
ConcurrentException
- if the cause of theExecutionException
is a checked exception
-
handleCauseUnchecked
Handles the specifiedExecutionException
and transforms it into a runtime exception. This method works exactly likehandleCause(ExecutionException)
, but instead of aConcurrentException
it throws aConcurrentRuntimeException
. This is an alternative for client code that does not want to deal with checked exceptions.- Parameters:
ex
- the exception to be handled- Throws:
ConcurrentRuntimeException
- if the cause of theExecutionException
is a checked exception; this exception is then wrapped in the thrown runtime exception
-
initialize
Invokes the specifiedConcurrentInitializer
and returns the object produced by the initializer. This method just invokes theget()
method of the givenConcurrentInitializer
. It is null-safe: if the argument is null, result is also null.- Type Parameters:
T
- the type of the object produced by the initializer- Parameters:
initializer
- theConcurrentInitializer
to be invoked- Returns:
- the object managed by the
ConcurrentInitializer
- Throws:
ConcurrentException
- if theConcurrentInitializer
throws an exception
-
initializeUnchecked
Invokes the specifiedConcurrentInitializer
and transforms occurring exceptions to runtime exceptions. This method works likeinitialize(ConcurrentInitializer)
, but if theConcurrentInitializer
throws aConcurrentException
, it is caught, and the cause is wrapped in aConcurrentRuntimeException
. So client code does not have to deal with checked exceptions.- Type Parameters:
T
- the type of the object produced by the initializer- Parameters:
initializer
- theConcurrentInitializer
to be invoked- Returns:
- the object managed by the
ConcurrentInitializer
- Throws:
ConcurrentRuntimeException
- if the initializer throws an exception
-
putIfAbsent
Puts a value in the specifiedConcurrentMap
if the key is not yet present. This method works similar to theputIfAbsent()
method of theConcurrentMap
interface, but the value returned is different. Basically, this method is equivalent to the following code fragment:if (!map.containsKey(key)) { map.put(key, value); return value; } else { return map.get(key); }
except that the action is performed atomically. So this method always returns the value which is stored in the map.
This method is null-safe: It accepts a null map as input without throwing an exception. In this case the return value is null, too.
- Type Parameters:
K
- the type of the keys of the mapV
- the type of the values of the map- Parameters:
map
- the map to be modifiedkey
- the key of the value to be addedvalue
- the value to be added- Returns:
- the value stored in the map after this operation
-