Top Description Inners Constructors Methods
org.apache.commons.lang3.concurrent

public Class ConcurrentUtils

extends Object
Class Inheritance
Imports
java.util.concurrent.ConcurrentMap, .ExecutionException, .Future, .TimeUnit, org.apache.commons.lang3.Validate, org.apache.commons.lang3.exception.ExceptionUtils

A utility class providing functionality related to the java.util.concurrent package.
Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
ConcurrentUtils.ConstantFuture<
the type of the value wrapped by this class
T
>

A specialized Future implementation which wraps a constant value.

Constructor Summary

AccessConstructor and Description
private
ConcurrentUtils()

Private constructor so that no instances can be created.

Method Summary

Modifier and TypeMethod and Description
pack-priv static Throwable

Returns:

a flag whether the passed in exception is a checked exception
checkedException
(final Throwable
the Throwable to check
ex
)

Tests whether the specified Throwable is a checked exception.

public static <
the type of the value used by this Future object
T
>
Future<T>

Returns:

an instance of Future that will return the value, never null
constantFuture
(final T
the constant value to return, may be null
value
)

Gets an implementation of Future that is immediately done and returns the specified constant value.

public static <
the type of the keys of the map
K
,
the type of the values of the map
V
>
V

Returns:

the value stored in the map after this operation; this may or may not be the object created by the ConcurrentInitializer
createIfAbsent
(final ConcurrentMap<K, V>
the map to be modified
map
,
final K
the key of the value to be added
key
,
final ConcurrentInitializer<V>
the ConcurrentInitializer for creating the value
init
)

Checks if a concurrent map contains a key and creates a corresponding value if not.

public static <
the type of the keys of the map
K
,
the type of the values of the map
V
>
V

Returns:

the value stored in the map after this operation; this may or may not be the object created by the ConcurrentInitializer
createIfAbsentUnchecked
(final ConcurrentMap<K, V>
the map to be modified
map
,
final K
the key of the value to be added
key
,
final ConcurrentInitializer<V>
the ConcurrentInitializer for creating the value
init
)

Checks if a concurrent map contains a key and creates a corresponding value if not, suppressing checked exceptions.

public static ConcurrentException

Returns:

a ConcurrentException with the checked cause
extractCause
(final ExecutionException
the exception to be processed
ex
)

Inspects the cause of the specified ExecutionException and creates a ConcurrentException with the checked cause if necessary.

public static ConcurrentRuntimeException

Returns:

a ConcurrentRuntimeException with the checked cause
extractCauseUnchecked
(final ExecutionException
the exception to be processed
ex
)

Inspects the cause of the specified ExecutionException and creates a ConcurrentRuntimeException with the checked cause if necessary.

public static void
handleCause(final ExecutionException
the exception to be handled
ex
)

Handles the specified ExecutionException.

public static void
handleCauseUnchecked(final ExecutionException
the exception to be handled
ex
)

Handles the specified ExecutionException and transforms it into a runtime exception.

public static <
the type of the object produced by the initializer
T
>
T

Returns:

the object managed by the ConcurrentInitializer
initialize
(final ConcurrentInitializer<T>
the ConcurrentInitializer to be invoked
initializer
)

Invokes the specified ConcurrentInitializer and returns the object produced by the initializer.

public static <
the type of the object produced by the initializer
T
>
T

Returns:

the object managed by the ConcurrentInitializer
initializeUnchecked
(final ConcurrentInitializer<T>
the ConcurrentInitializer to be invoked
initializer
)

Invokes the specified ConcurrentInitializer and transforms occurring exceptions to runtime exceptions.

public static <
the type of the keys of the map
K
,
the type of the values of the map
V
>
V

Returns:

the value stored in the map after this operation
putIfAbsent
(final ConcurrentMap<K, V>
the map to be modified
map
,
final K
the key of the value to be added
key
,
final V
the value to be added
value
)

Puts a value in the specified ConcurrentMap if the key is not yet present.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ConcurrentUtilsback to summary
private ConcurrentUtils()

Private constructor so that no instances can be created. This class contains only static utility methods.

Method Detail

checkedExceptionback to summary
pack-priv static Throwable checkedException(final Throwable ex)

Tests whether the specified Throwable is a checked exception. If not, an exception is thrown.

Parameters
ex:Throwable

the Throwable to check

Returns:Throwable

a flag whether the passed in exception is a checked exception

Exceptions
IllegalArgumentException:
if the Throwable is not a checked exception
constantFutureback to summary
public static <T> Future<T> constantFuture(final T value)

Gets an implementation of Future 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.

Parameters
<T>
the type of the value used by this Future object
value:T

the constant value to return, may be null

Returns:Future<T>

an instance of Future that will return the value, never null

createIfAbsentback to summary
public static <K, V> V createIfAbsent(final ConcurrentMap<K, V> map, final K key, final ConcurrentInitializer<V> init) throws ConcurrentException

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 in ConcurrentInitializer is 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 be null; in this case this method simply returns null.

Parameters
<K>
the type of the keys of the map
<V>
the type of the values of the map
map:ConcurrentMap<K, V>

the map to be modified

key:K

the key of the value to be added

init:ConcurrentInitializer<V>

the ConcurrentInitializer for creating the value

Returns:V

the value stored in the map after this operation; this may or may not be the object created by the ConcurrentInitializer

Exceptions
ConcurrentException:
if the initializer throws an exception
createIfAbsentUncheckedback to summary
public static <K, V> V createIfAbsentUnchecked(final ConcurrentMap<K, V> map, final K key, final ConcurrentInitializer<V> init)

Checks if a concurrent map contains a key and creates a corresponding value if not, suppressing checked exceptions. This method calls createIfAbsent(). If a ConcurrentException is thrown, it is caught and re-thrown as a ConcurrentRuntimeException.

Parameters
<K>
the type of the keys of the map
<V>
the type of the values of the map
map:ConcurrentMap<K, V>

the map to be modified

key:K

the key of the value to be added

init:ConcurrentInitializer<V>

the ConcurrentInitializer for creating the value

Returns:V

the value stored in the map after this operation; this may or may not be the object created by the ConcurrentInitializer

Exceptions
ConcurrentRuntimeException:
if the initializer throws an exception
extractCauseback to summary
public static ConcurrentException extractCause(final ExecutionException ex)

Inspects the cause of the specified ExecutionException and creates a ConcurrentException 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:ExecutionException

the exception to be processed

Returns:ConcurrentException

a ConcurrentException with the checked cause

extractCauseUncheckedback to summary
public static ConcurrentRuntimeException extractCauseUnchecked(final ExecutionException ex)

Inspects the cause of the specified ExecutionException and creates a ConcurrentRuntimeException with the checked cause if necessary. This method works exactly like extractCause(ExecutionException). The only difference is that the cause of the specified ExecutionException is extracted as a runtime exception. This is an alternative for client code that does not want to deal with checked exceptions.

Parameters
ex:ExecutionException

the exception to be processed

Returns:ConcurrentRuntimeException

a ConcurrentRuntimeException with the checked cause

handleCauseback to summary
public static void handleCause(final ExecutionException ex) throws ConcurrentException

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 a ConcurrentException, which is thrown. If the passed in exception is null or has no cause, the method simply returns without throwing an exception.

Parameters
ex:ExecutionException

the exception to be handled

Exceptions
ConcurrentException:
if the cause of the ExecutionException is a checked exception
handleCauseUncheckedback to summary
public static void handleCauseUnchecked(final ExecutionException ex)

Handles the specified ExecutionException and transforms it into a runtime exception. This method works exactly like handleCause(ExecutionException), but instead of a ConcurrentException it throws a ConcurrentRuntimeException. This is an alternative for client code that does not want to deal with checked exceptions.

Parameters
ex:ExecutionException

the exception to be handled

Exceptions
ConcurrentRuntimeException:
if the cause of the ExecutionException is a checked exception; this exception is then wrapped in the thrown runtime exception
initializeback to summary
public static <T> T initialize(final ConcurrentInitializer<T> initializer) throws ConcurrentException

Invokes the specified ConcurrentInitializer and returns the object produced by the initializer. This method just invokes the get() method of the given ConcurrentInitializer. It is null-safe: if the argument is null, result is also null.

Parameters
<T>
the type of the object produced by the initializer
initializer:ConcurrentInitializer<T>

the ConcurrentInitializer to be invoked

Returns:T

the object managed by the ConcurrentInitializer

Exceptions
ConcurrentException:
if the ConcurrentInitializer throws an exception
initializeUncheckedback to summary
public static <T> T initializeUnchecked(final ConcurrentInitializer<T> initializer)

Invokes the specified ConcurrentInitializer and transforms occurring exceptions to runtime exceptions. This method works like initialize(ConcurrentInitializer), but if the ConcurrentInitializer throws a ConcurrentException, it is caught, and the cause is wrapped in a ConcurrentRuntimeException. So client code does not have to deal with checked exceptions.

Parameters
<T>
the type of the object produced by the initializer
initializer:ConcurrentInitializer<T>

the ConcurrentInitializer to be invoked

Returns:T

the object managed by the ConcurrentInitializer

Exceptions
ConcurrentRuntimeException:
if the initializer throws an exception
putIfAbsentback to summary
public static <K, V> V putIfAbsent(final ConcurrentMap<K, V> map, final K key, final V value)

Puts a value in the specified ConcurrentMap if the key is not yet present. This method works similar to the putIfAbsent() method of the ConcurrentMap 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.

Parameters
<K>
the type of the keys of the map
<V>
the type of the values of the map
map:ConcurrentMap<K, V>

the map to be modified

key:K

the key of the value to be added

value:V

the value to be added

Returns:V

the value stored in the map after this operation

org.apache.commons.lang3.concurrent back to summary

pack-priv final Class ConcurrentUtils.ConstantFuture<T>

extends Object
implements Future<T>
Class Inheritance
  • java.lang.Object
  • org.apache.commons.lang3.concurrent.ConcurrentUtils.ConstantFuture
All Implemented Interfaces
java.util.concurrent.Future
Type Parameters
<T>
the type of the value wrapped by this class

A specialized Future implementation which wraps a constant value.

Field Summary

Modifier and TypeField and Description
private final T
value

The constant value.

Constructor Summary

AccessConstructor and Description
pack-priv
ConstantFuture(final T
the value (may be null)
value
)

Creates a new instance of ConstantFuture and initializes it with the constant value.

Method Summary

Modifier and TypeMethod and Description
public boolean
cancel(final boolean mayInterruptIfRunning)

Implements java.util.concurrent.Future.cancel.

Attempts to cancel execution of this task.

public T
get()

Implements java.util.concurrent.Future.get.

Waits if necessary for the computation to complete, and then retrieves its result.

public T
get(final long timeout, final TimeUnit unit)

Implements java.util.concurrent.Future.get.

Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.

public boolean
isCancelled()

Implements java.util.concurrent.Future.isCancelled.

Returns true if this task was cancelled before it completed normally.

public boolean
isDone()

Implements java.util.concurrent.Future.isDone.

Returns true if this task completed.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

valueback to summary
private final T value

The constant value.

Constructor Detail

ConstantFutureback to summary
pack-priv ConstantFuture(final T value)

Creates a new instance of ConstantFuture and initializes it with the constant value.

Parameters
value:T

the value (may be null)

Method Detail

cancelback to summary
public boolean cancel(final boolean mayInterruptIfRunning)

Implements java.util.concurrent.Future.cancel.

Doc from java.util.concurrent.Future.cancel.

Attempts to cancel execution of this task. This method has no effect if the task is already completed or cancelled, or could not be cancelled for some other reason. Otherwise, if this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task (when known by the implementation) is interrupted in an attempt to stop the task.

The return value from this method does not necessarily indicate whether the task is now cancelled; use isCancelled. The cancel operation is not supported. This implementation always returns false.

Annotations
@Override
getback to summary
public T get()

Implements java.util.concurrent.Future.get.

Doc from java.util.concurrent.Future.get.

Waits if necessary for the computation to complete, and then retrieves its result. This implementation just returns the constant value.

Annotations
@Override
getback to summary
public T get(final long timeout, final TimeUnit unit)

Implements java.util.concurrent.Future.get.

Doc from java.util.concurrent.Future.get.

Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available. This implementation just returns the constant value; it does not block, therefore the timeout has no meaning.

Annotations
@Override
isCancelledback to summary
public boolean isCancelled()

Implements java.util.concurrent.Future.isCancelled.

Doc from java.util.concurrent.Future.isCancelled.

Returns true if this task was cancelled before it completed normally. This implementation always returns false; there is no background process which could be cancelled.

Annotations
@Override
isDoneback to summary
public boolean isDone()

Implements java.util.concurrent.Future.isDone.

Doc from java.util.concurrent.Future.isDone.

Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true. This implementation always returns true because the constant object managed by this Future implementation is always available.

Annotations
@Override