Class EolContextParallel

  • All Implemented Interfaces:
    IEolContextParallel, IEolContext
    Direct Known Subclasses:
    ErlContextParallel, EwlContextParallel

    public class EolContextParallel
    extends EolContext
    implements IEolContextParallel
    Skeletal implementation of a parallel IEolContext. This class takes care of the common structures which are affected by multi-threading using ThreadLocals. For use cases where these thread-local values are required, a PersistentThreadLocal is used, so that the context will be consistent with a sequential implementation once #endParallel() is invoked.

    For optimal performance, it is recommend that parallel tasks obtain a sequential "snapshot" of this context to avoid frequent retrieval of ThreadLocal values using the getShadow() method.
    Sina Madani
    • Constructor Detail

      • EolContextParallel

        public EolContextParallel()
      • EolContextParallel

        public EolContextParallel​(int parallelism)
        parallelism - The number of threads to use.
      • EolContextParallel

        protected EolContextParallel​(IEolContext other)
        Copy constructor.
        other - The parent context to copy from. Must not be null.
    • Method Detail

      • initThreadLocals

        protected void initThreadLocals()
      • parallelGet

        protected final <R> R parallelGet​(java.lang.ThreadLocal<? extends R> threadLocal,
                                          java.util.function.Supplier<? extends R> originalValueGetter)
        Utility method used to appropriately return either a thread-local or the original value, depending on whether this context isParallel().
        Type Parameters:
        R - The value type
        threadLocal - The ThreadLocal value (returned if parallel).
        originalValueGetter - The non-thread-local value (returned if not parallel).
        The appropriate value for the current thread.
      • parallelGet

        protected final <R> R parallelGet​(java.lang.ThreadLocal<? extends R> threadLocal,
                                          R originalValue)
        Utility method used to appropriately return either a thread-local or the original value, depending on whether this context isParallel().
        Type Parameters:
        R - The value type
        threadLocal - The thread-local wrapper for the value
        originalValue - The main, persistent variable
        The appropriate value for the current thread.
      • parallelSet

        protected final <T> void parallelSet​(T value,
                                             java.lang.ThreadLocal<? super T> threadLocal,
                                             java.util.function.Consumer<? super T> originalValueSetter)
        Utility method used to appropriately set either a thread-local or the original value, depending on whether this context isParallel().
        value - The value to set.
        threadLocal - The ThreadLocal value (will be set if parallel).
        originalValueSetter - The non-thread-local value (will be set if not parallel).
      • removeAll

        protected void removeAll​(java.lang.ThreadLocal<?>... threadLocals)
      • clearThreadLocals

        protected void clearThreadLocals()
      • nullifyThreadLocals

        protected void nullifyThreadLocals()
      • clearExecutor

        protected void clearExecutor()
      • setParallelism

        public void setParallelism​(int threads)
                            throws java.lang.IllegalStateException,
        Description copied from interface: IEolContextParallel
        Attempts to set the parallelism of this context and its associated executor. Note that this may not take effect immediately and is intended to be called during parallel execution. Implementations may ignore this operation or throw an UnsupportedOperationException. It is recommended that this method is only called during initialisation, and is present for convenience only.
        Specified by:
        setParallelism in interface IEolContextParallel
        threads - The new value. Must be positive.
        java.lang.IllegalStateException - If this method is called at an inconvenient time.
        java.lang.IllegalArgumentException - If the new value is out of bounds.
      • getParallelism

        public final int getParallelism()
        Description copied from interface: IEolContextParallel
        Indicates the scalability of this Context when more processing nodes are added.
        Specified by:
        getParallelism in interface IEolContextParallel
        the number of threads.
      • toString

        public java.lang.String toString()
        toString in class java.lang.Object
      • createThreadLocalExecutorFactory

        protected ExecutorFactory createThreadLocalExecutorFactory()
      • createThreadLocalFrameStack

        protected FrameStack createThreadLocalFrameStack()
      • createShadowThreadLocalContext

        protected IEolContext createShadowThreadLocalContext()
      • getShadow

        public IEolContext getShadow()
        Can be used to obtain an optimal execution context while executing in parallel. If execution is currently not parallel, then this context itself is returned.
        A ThreadLocal copy of this context if in parallel, or this context otherwise.
      • convertToParallel

        public static IEolContextParallel convertToParallel​(IEolContext context)
                                                     throws EolNestedParallelismException
        Utility method for converting a sequential context to a parallel one, if it is not already parallel.
        context - The current execution context to check or convert.
        The context if it is already parallel, or a new EolContextParallel with state copied over from the context parameter.
        EolNestedParallelismException - If the context is already derived from a parallel context.
      • executeJob

        public java.lang.Object executeJob​(java.lang.Object job)
                                    throws EolRuntimeException
        Evaluates the job using this context's parallel execution facilities. Subclasses may override this to support additional job types, calling the super method as the last resort for unknown cases. Supported types include multi-valued types (e.g. arrays, Iterable / Iterator, Stream etc.) as well as common concurrency units such as Runnable, Callable and Future.
        job - The job (or jobs) to evaluate.
        The result of evaluating the job, if any.
        java.lang.IllegalArgumentException - If the job type is not recognised.
        EolRuntimeException - If an exception is thrown whilst evaluating the job(s).