ConcurrentUtils¶
- public class org.apache.commons.lang3.concurrent.ConcurrentUtils[source]¶
A utility class providing functionality related to the
java.util.concurrentpackage.- Since:
3.0
Methods¶
Gets an implementation of |
|
|
Checks if a concurrent map contains a key and creates a corresponding value if not. |
|
Checks if a concurrent map contains a key and creates a corresponding value if not, suppressing checked exceptions. |
Inspects the cause of the specified |
|
Inspects the cause of the specified |
|
Handles the specified |
|
Handles the specified |
|
Invokes the specified |
|
Invokes the specified |
|
Puts a value in the specified |
- public static <T> Future<T> constantFuture(T value)[source]¶
Gets an implementation of
Futurethat 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 this
Futureobject- Parameters:
value – The constant value to return, may be null
- Returns:
An instance of Future that will return the value, never null
- public static <K, V> V createIfAbsent(ConcurrentMap<K, V> map, K key, ConcurrentInitializer<V> init) throws ConcurrentException[source]¶
Checks 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 the
get()method of the passed inConcurrentInitializeris called. With the resulting object#putIfAbsent(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 benull; in this case this method simply returnsnull.- Type parameters:
<K> – The type of the keys of the map
<V> – The type of the values of the map
- Parameters:
map – The map to be modified
key – The key of the value to be added
init – The
ConcurrentInitializerfor 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:
org.apache.commons.lang3.concurrent.ConcurrentException – Thrown if the initializer throws an exception.
- public static <K, V> V createIfAbsentUnchecked(ConcurrentMap<K, V> map, K key, ConcurrentInitializer<V> init)[source]¶
Checks if a concurrent map contains a key and creates a corresponding value if not, suppressing checked exceptions. This method calls
createIfAbsent(). If aConcurrentExceptionis thrown, it is caught and re-thrown as aConcurrentRuntimeException.- Type parameters:
<K> – The type of the keys of the map
<V> – The type of the values of the map
- Parameters:
map – The map to be modified
key – The key of the value to be added
init – The
ConcurrentInitializerfor 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:
org.apache.commons.lang3.concurrent.ConcurrentRuntimeException – Thrown if the initializer throws an exception.
- public static ConcurrentException extractCause(ExecutionException ex)[source]¶
Inspects the cause of the specified
ExecutionExceptionand creates aConcurrentExceptionwith 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
nullor the cause isnull, this method returnsnull.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
ConcurrentExceptionwith the checked cause
- public static ConcurrentRuntimeException extractCauseUnchecked(ExecutionException ex)[source]¶
Inspects the cause of the specified
ExecutionExceptionand creates aConcurrentRuntimeExceptionwith the checked cause if necessary. This method works exactly like#extractCause(ExecutionException). The only difference is that the cause of the specifiedExecutionExceptionis 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
ConcurrentRuntimeExceptionwith the checked cause
- public static void handleCause(ExecutionException ex) throws ConcurrentException[source]¶
Handles the specified
ExecutionException. This method calls#extractCause(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 isnullor has no cause, the method simply returns without throwing an exception.- Parameters:
ex – The exception to be handled
- Throws:
org.apache.commons.lang3.concurrent.ConcurrentException – Thrown if the cause of the
ExecutionExceptionis a checked exception.
- public static void handleCauseUnchecked(ExecutionException ex)[source]¶
Handles the specified
ExecutionExceptionand transforms it into a runtime exception. This method works exactly like#handleCause(ExecutionException), but instead of aConcurrentExceptionit 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:
org.apache.commons.lang3.concurrent.ConcurrentRuntimeException – Thrown if the cause of the
ExecutionExceptionis a checked exception; this exception is then wrapped in the thrown runtime exception.
- public static <T> T initialize(ConcurrentInitializer<T> initializer) throws ConcurrentException[source]¶
Invokes the specified
ConcurrentInitializerand returns the object produced by the initializer. This method just invokes theget()method of the givenConcurrentInitializer. It isnull-safe: if the argument isnull, result is alsonull.- Type parameters:
<T> – The type of the object produced by the initializer
- Parameters:
initializer – The
ConcurrentInitializerto be invoked- Returns:
The object managed by the
ConcurrentInitializer- Throws:
org.apache.commons.lang3.concurrent.ConcurrentException – Thrown if the
ConcurrentInitializerthrows an exception.
- public static <T> T initializeUnchecked(ConcurrentInitializer<T> initializer)[source]¶
Invokes the specified
ConcurrentInitializerand transforms occurring exceptions to runtime exceptions. This method works like#initialize(ConcurrentInitializer), but if theConcurrentInitializerthrows 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 – The
ConcurrentInitializerto be invoked- Returns:
The object managed by the
ConcurrentInitializer- Throws:
org.apache.commons.lang3.concurrent.ConcurrentRuntimeException – Thrown if the initializer throws an exception.
- public static <K, V> V putIfAbsent(ConcurrentMap<K, V> map, K key, V value)[source]¶
Puts a value in the specified
ConcurrentMapif the key is not yet present. This method works similar to theputIfAbsent()method of theConcurrentMapinterface, 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 anullmap as input without throwing an exception. In this case the return value isnull, too.- Type parameters:
<K> – The type of the keys of the map
<V> – The type of the values of the map
- Parameters:
map – The map to be modified
key – The key of the value to be added
value – The value to be added
- Returns:
The value stored in the map after this operation