public class Reducible<E>
extends java.lang.Object
Note that the number of thread-local values used in the reduction is determined according to the number of unique
Thread
s that set(Object)
a value.
Constructor and Description |
---|
Reducible()
A new
Reducible without an initial value for any of the threads. |
Reducible(E initialValue)
Creates a
Reducible with the specified initial value for all threads. |
Modifier and Type | Method and Description |
---|---|
int |
countOfThreadLocalValues()
Returns the number of thread-local values currently stored.
|
E |
get()
Returns the thread-local value for the current thread.
|
E |
reduce(Reduction<E> reduction)
Performs the specified reduction, as defined by the
reduction instance. |
void |
set(E value)
Sets the thread-local value of the current thread to
value . |
public Reducible()
Reducible
without an initial value for any of the threads. Therefore, each thread must call
set(Object)
before attempting to get()
its thread-local value.public Reducible(E initialValue)
Reducible
with the specified initial value for all threads.initialValue
- The initial value to set for all threads.public void set(E value)
value
.value
- The value to set for the current thread.public E get()
set(Object)
for the current thread.java.lang.RuntimeException
- If no initial value was supplied in the constructor and
no value has been set(Object)
for the current thread.public int countOfThreadLocalValues()
set(Object)
.public E reduce(Reduction<E> reduction)
reduction
instance.
This method should be called at the end when all thread-local values are ready. Note that a reduction is only calculated once, therefore subsequent calls to this method result in the pre-calculated value being returned.
reduction
- The reduction to perform.java.lang.RuntimeException
- If attempting to perform a reduction but there are no values that have been set
(either by specifying an initial value or using set(Object)
).