Class ThreadedInstantiating<E>

java.lang.Object
one.microstream.concurrency.Threaded<E>
one.microstream.concurrency.ThreadedInstantiating<E>
All Implemented Interfaces:
ConsolidatableCollection, OptimizableCollection, Sized, Referencing<E>

public class ThreadedInstantiating<E>
extends Threaded<E>
Extended 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 Details

    • ThreadedInstantiating

      public ThreadedInstantiating​(Instantiator<E> instantiator) throws NullPointerException
      Instantiates a new empty 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).

      Parameters:
      instantiator - the Instantiator instance to be used to create to be associated instances of type E.
      Throws:
      NullPointerException - if the passed Instantiator instance is null.
      See Also:
      ThreadedInstantiating(Instantiator, int)
    • ThreadedInstantiating

      public ThreadedInstantiating​(Instantiator<E> instantiator, int initialCapacity) throws NullPointerException
      Instantiates a new empty 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).

      Parameters:
      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.
      Throws:
      NullPointerException - if the passed Instantiator instance is null.
  • Method Details

    • threaded

      public static final <E> ThreadedInstantiating<E> threaded​(Instantiator<E> instantiator)
      Convenience / readability method that wraps the passed 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
           }
      
       }
       
      Type Parameters:
      E - the type of the instances created by the passed Instantiator instance.
      Parameters:
      instantiator - the Instantiator instance to be used to create thread local instances of type E.
      Returns:
      a new ThreadedInstantiating instance wrapping the passed Instantiator instance.
    • getInstantiator

      public 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.
      Returns:
      the wrapped Instantiator instance.
    • lookupMissFallbackElement

      protected E lookupMissFallbackElement()
      Locks the wrapped 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.

      Overrides:
      lookupMissFallbackElement in class Threaded<E>
      Returns:
      a newly created and associated instance of type E.
      See Also:
      Threaded.lookupMissFallbackElement(), Threaded.get()