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 emptyThreadedInstantiating
instance with the passedInstantiator
instance and default (quite small) storage size.ThreadedInstantiating(Instantiator<E> instantiator, int initialCapacity)
Instantiates a new emptyThreadedInstantiating
instance with the passedInstantiator
instance and a storage size of at least the passed value. -
Method Summary
Modifier and Type Method Description Instantiator<E>
getInstantiator()
Returns the wrappedInstantiator
instance used by this instance to create instances of type E to be associated with the currentThread
if no association could have been found for it.static <E> ThreadedInstantiating<E>
threaded(Instantiator<E> instantiator)
Convenience / readability method that wraps the passedInstantiator
instance in a newThreadedInstantiating
instance.Methods inherited from class one.microstream.concurrency.Threaded
consolidate, containsSearched, get, getCleanUpOperation, isEmpty, New, New, optimize, remove, set, setCleanUpOperation, size
-
Constructor Details
-
ThreadedInstantiating
Instantiates a new emptyThreadedInstantiating
instance with the passedInstantiator
instance and default (quite small) storage size.Note that an
Instantiator
instance that returns the same instance of type E for more than oneThread
, apart from breaking theInstantiator
contract to create a new instance on every call, defeats the purpose of aThreaded
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).- Parameters:
instantiator
- theInstantiator
instance to be used to create to be associated instances of type E.- Throws:
NullPointerException
- if the passedInstantiator
instance isnull
.- See Also:
ThreadedInstantiating(Instantiator,int)
-
ThreadedInstantiating
public ThreadedInstantiating(Instantiator<E> instantiator, int initialCapacity) throws NullPointerExceptionInstantiates a new emptyThreadedInstantiating
instance with the passedInstantiator
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 oneThread
, apart from breaking theInstantiator
contract to create a new instance on every call, defeats the purpose of aThreaded
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).- Parameters:
instantiator
- theInstantiator
instance to be used to create to be associated instances of type E.initialCapacity
- the minimal storage size theThreadedInstantiating
instance gets allocated with.- Throws:
NullPointerException
- if the passedInstantiator
instance isnull
.
-
-
Method Details
-
threaded
Convenience / readability method that wraps the passedInstantiator
instance in a newThreadedInstantiating
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 } } }- Type Parameters:
E
- the type of the instances created by the passedInstantiator
instance.- Parameters:
instantiator
- theInstantiator
instance to be used to create thread local instances of type E.- Returns:
- a new
ThreadedInstantiating
instance wrapping the passedInstantiator
instance.
-
getInstantiator
Returns the wrappedInstantiator
instance used by this instance to create instances of type E to be associated with the currentThread
if no association could have been found for it.- Returns:
- the wrapped
Instantiator
instance.
-