public class ThreadedInstantiating<E> extends Threaded<E>
Threaded class that wraps a Instantiator instance to automatically create a new
instance of type E to be associated with the current Thread if Threaded.get() could not find an
existing association for it.
Example: (also note the missing initialize() method compared to Threaded example)
public class SomeClass
{
private final Threaded<StringBuilder> sb = threadLocal(sbFactory); // conveniently short
// ...
private void doStuff()
{
sb.get().append("stuff"); // uses each thread's own exclusive StringBuilder instance
}
public String toString()
{
return sb.get().toString(); // the current thread's constructed String
}
}
| Constructor and Description |
|---|
ThreadedInstantiating(Instantiator<E> instantiator)
Instantiates a new empty
ThreadedInstantiating instance with the passed Instantiator instance
and default (quite small) storage size. |
ThreadedInstantiating(Instantiator<E> instantiator,
int initialCapacity)
Instantiates a new empty
ThreadedInstantiating instance with the passed Instantiator instance
and a storage size of at least the passed value. |
| Modifier and Type | Method and Description |
|---|---|
Instantiator<E> |
getInstantiator()
Returns the wrapped
Instantiator instance used by this instance to create instances of
type E to be associated with the current Thread if no association could have been found for it. |
protected E |
lookupMissFallbackElement()
Locks the wrapped
Instantiator instance and uses it to create a new instance of type E. |
static <E> ThreadedInstantiating<E> |
threaded(Instantiator<E> instantiator)
Convenience / readability method that wraps the passed
Instantiator instance in a new
ThreadedInstantiating instance. |
addForCurrentThread, consolidate, containsSearched, get, getCleanUpOperation, isEmpty, New, New, optimize, remove, set, setCleanUpOperation, sizepublic ThreadedInstantiating(Instantiator<E> instantiator) throws NullPointerException
ThreadedInstantiating instance with the passed Instantiator instance
and default (quite small) storage size.
Note that an Instantiator instance that returns the same instance of type E for more than one
Thread, apart from breaking the Instantiator contract to create a new instance on every call,
defeats the purpose of a Threaded in the first place.
Still, such behavior won't cause any (direct) error in this class (and may be reasonable in certain situations
despite all concerns).
instantiator - the Instantiator instance to be used to create to be associated instances of type E.NullPointerException - if the passed Instantiator instance is null.ThreadedInstantiating(Instantiator, int)public ThreadedInstantiating(Instantiator<E> instantiator, int initialCapacity) throws NullPointerException
ThreadedInstantiating instance with the passed Instantiator instance
and a storage size of at least the passed value.
The created instance is guaranteed to be able to store an amount of associations equal to the passed value before a storage rebuild occurs.
Note that an Instantiator instance that returns the same instance of type E for more than one
Thread, apart from breaking the Instantiator contract to create a new instance on every call,
defeats the purpose of a Threaded in the first place.
Still, such behavior won't cause any (direct) error in this class (and may be reasonable in certain situations
despite all concerns).
Also note that the internal storage size can drop below the passed value (to the same size used by
ThreadedInstantiating(Instantiator)) if at some point the optimizing algorithm detects that a smaller
storage size will suffice. This is guaranteed not to happen before the storage size allocated depending on the
passed value had to be increased at least once (i.e. the initial capacity remains guaranteed for the initial life
time of the created instance).
instantiator - the Instantiator instance to be used to create to be associated instances of type E.initialCapacity - the minimal storage size the ThreadedInstantiating instance gets allocated with.NullPointerException - if the passed Instantiator instance is null.public static final <E> ThreadedInstantiating<E> threaded(Instantiator<E> instantiator)
Instantiator instance in a new
ThreadedInstantiating instance.
Example: (also note the missing initialize() method compared to Threaded example)
public class SomeClass
{
private final Threaded<StringBuilder> sb = threadLocal(sbFactory); // conveniently short
// ...
private void doStuff()
{
sb.get().append("stuff"); // uses each thread's own exclusive StringBuilder instance
}
public String toString()
{
return sb.get().toString(); // the current thread's constructed String
}
}
E - the type of the instances created by the passed Instantiator instance.instantiator - the Instantiator instance to be used to create thread local instances of type E.ThreadedInstantiating instance wrapping the passed Instantiator instance.public Instantiator<E> getInstantiator()
Instantiator instance used by this instance to create instances of
type E to be associated with the current Thread if no association could have been found for it.Instantiator instance.protected E lookupMissFallbackElement()
Instantiator instance and uses it to create a new instance of type E. The new instance
is then associated with the current Thread and returned.
This method is called by Threaded.get() if no association for the current Thread could have been found.
lookupMissFallbackElement in class Threaded<E>Threaded.lookupMissFallbackElement(),
Threaded.get()Copyright © 2022 MicroStream Software. All rights reserved.