Class ThreadedInstantiating<E>
- All Implemented Interfaces:
ConsolidatableCollection,OptimizableCollection,Sized,Referencing<E>
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 Summary
Constructors Constructor Description ThreadedInstantiating(Instantiator<E> instantiator)Instantiates a new emptyThreadedInstantiatinginstance with the passedInstantiatorinstance and default (quite small) storage size.ThreadedInstantiating(Instantiator<E> instantiator, int initialCapacity)Instantiates a new emptyThreadedInstantiatinginstance with the passedInstantiatorinstance and a storage size of at least the passed value. -
Method Summary
Modifier and Type Method Description Instantiator<E>getInstantiator()Returns the wrappedInstantiatorinstance used by this instance to create instances of type E to be associated with the currentThreadif no association could have been found for it.protected ElookupMissFallbackElement()Locks the wrappedInstantiatorinstance 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 passedInstantiatorinstance in a newThreadedInstantiatinginstance.Methods inherited from class one.microstream.concurrency.Threaded
addForCurrentThread, consolidate, containsSearched, get, getCleanUpOperation, isEmpty, New, New, optimize, remove, set, setCleanUpOperation, size
-
Constructor Details
-
ThreadedInstantiating
Instantiates a new emptyThreadedInstantiatinginstance with the passedInstantiatorinstance and default (quite small) storage size.Note that an
Instantiatorinstance that returns the same instance of type E for more than oneThread, apart from breaking theInstantiatorcontract to create a new instance on every call, defeats the purpose of aThreadedin 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).- Parameters:
instantiator- theInstantiatorinstance to be used to create to be associated instances of type E.- Throws:
NullPointerException- if the passedInstantiatorinstance isnull.- See Also:
ThreadedInstantiating(Instantiator, int)
-
ThreadedInstantiating
public ThreadedInstantiating(Instantiator<E> instantiator, int initialCapacity) throws NullPointerExceptionInstantiates a new emptyThreadedInstantiatinginstance with the passedInstantiatorinstance 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
Instantiatorinstance that returns the same instance of type E for more than oneThread, apart from breaking theInstantiatorcontract to create a new instance on every call, defeats the purpose of aThreadedin 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).- Parameters:
instantiator- theInstantiatorinstance to be used to create to be associated instances of type E.initialCapacity- the minimal storage size theThreadedInstantiatinginstance gets allocated with.- Throws:
NullPointerException- if the passedInstantiatorinstance isnull.
-
-
Method Details
-
threaded
Convenience / readability method that wraps the passedInstantiatorinstance in a newThreadedInstantiatinginstance.Example: (also note the missing initialize() method compared to
Threadedexample)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 } }- Type Parameters:
E- the type of the instances created by the passedInstantiatorinstance.- Parameters:
instantiator- theInstantiatorinstance to be used to create thread local instances of type E.- Returns:
- a new
ThreadedInstantiatinginstance wrapping the passedInstantiatorinstance.
-
getInstantiator
Returns the wrappedInstantiatorinstance used by this instance to create instances of type E to be associated with the currentThreadif no association could have been found for it.- Returns:
- the wrapped
Instantiatorinstance.
-
lookupMissFallbackElement
Locks the wrappedInstantiatorinstance and uses it to create a new instance of type E. The new instance is then associated with the currentThreadand returned.This method is called by
Threaded.get()if no association for the currentThreadcould have been found.- Overrides:
lookupMissFallbackElementin classThreaded<E>- Returns:
- a newly created and associated instance of type E.
- See Also:
Threaded.lookupMissFallbackElement(),Threaded.get()
-