Interface RunWithConcurrency

    • Field Detail

      • DEFAULT_MAX_NUMBER_OF_RETRIES

        static final long DEFAULT_MAX_NUMBER_OF_RETRIES
        Default value for how often a task is retried before giving up. The default is so that retrying every microsecond will stop after about 3 days.
        See Also:
        maxWaitRetries(), Constant Field Values
    • Method Detail

      • concurrency

        int concurrency()
        The maximum concurrency for running the tasks.

        If the concurrency is 1, the tasks are run sequentially on the calling thread until all tasks are finished or the first Exception is thrown. This behavior can be overridden by setting forceUsageOfExecutor() to true.

      • tasks

        java.util.Iterator<? extends java.lang.Runnable> tasks()
        The tasks that will be executed.

        The tasks are submitted to the executor() only if they can be immediately scheduled and executed. The iterator is only iterated to at most one item in advance. If the task iterator creates the tasks lazily upon iteration, we can avoid creating all tasks upfront. We can support thousands, even millions of tasks without resource exhaustion that way.

        We will try to submit tasks as long as no more than concurrency() are already started and then continue to submit tasks one-by-one, after a previous tasks has finished, so that no more than concurrency tasks are running in the provided executor.

        We will try to submit tasks as long as the executor can directly start new tasks, that is, we want to avoid creating tasks and put them into the waiting queue if it may never be executed afterwards.

      • forceUsageOfExecutor

        @Default
        default boolean forceUsageOfExecutor()
        This setting is only relevant if the concurrency() is 1.

        If forceUsageOfExecutor is true, we will submit all tasks to the executor. If forceUsageOfExecutor is false, we will run all tasks on the calling thread.

        Running tasks on the calling thread will exit early once an exception is thrown from a task. Running tasks on the executor will collect all exceptions and run all non failing tasks.

        The default value is false and will let tasks run on the calling thread.

      • waitTime

        @Default
        default long waitTime()
        If the executor() is not able to accept any more tasks, we will wait and retry submitting the task. The time to wait is set by waitTime together with waitTimeUnit(). The number of retries is set by maxWaitRetries().

        After maxWaitRetries have been exhausted, execution of all further tasks is abandoned and a IllegalThreadStateException is thrown.

        The default is to retry every 1 microsecond for about 3 days.

      • waitTimeUnit

        @Default
        default java.util.concurrent.TimeUnit waitTimeUnit()
        If the executor() is not able to accept any more tasks, we will wait and retry submitting the task. The time to wait is set by waitTime() together with waitTimeUnit. The number of retries is set by maxWaitRetries().

        After maxWaitRetries have been exhausted, execution of all further tasks is abandoned and a IllegalThreadStateException is thrown.

        The default is to retry every 1 microsecond for about 3 days.

      • waitNanos

        @Derived
        default long waitNanos()
        The actual wait time in nanoseconds.
        See Also:
        waitTime(), waitTimeUnit()
      • maxWaitRetries

        @Default
        default long maxWaitRetries()
        If the executor() is not able to accept any more tasks, we will wait and retry submitting the task. The time to wait is set by waitTime together with waitTimeUnit(). The number of retries is set by maxWaitRetries.

        After maxWaitRetries have been exhausted, execution of all further tasks is abandoned and a IllegalThreadStateException is thrown.

        The default is to retry every 1 microsecond for about 3 days.

      • terminationFlag

        @Default
        default TerminationFlag terminationFlag()
        Provide a TerminationFlag to support graceful early termination.

        After the initial number of concurrency() tasks have been submitted, the termination flag is checked before submitting any further tasks. If the termination flag triggers, running tasks are asked to stop and outstanding tasks will be abandoned.

        It is up to the task implementation to also respect the termination flag.

        The default flag will never trigger.

      • mayInterruptIfRunning

        @Default
        default boolean mayInterruptIfRunning()
        If the terminationFlag() triggers or the calling thread is interrupted, all running tasks are cancelled. This flag is passed to Future.cancel(boolean).

        If mayInterruptIfRunning is true, running tasks are interrupted and may exit early if the InterruptedException is handled properly. If mayInterruptIfRunning is false, running tasks are allowed to finish on their own.

        In either case, tasks that have not been started will never start.

        The default behavior is to interrupt running tasks (true).

      • executor

        @Default
        @Nullable
        default @Nullable java.util.concurrent.ExecutorService executor()
        The executor that will run the tasks.

        If the executor is terminated, tasks are run on the calling thread, even for concurrencies greater than 1.

        If forceUsageOfExecutor() is true however, an IllegalArgumentException is thrown when this object is constructed.

        The default executor is Pools.DEFAULT.

      • run

        default void run()
        Try to run all tasks for their side effects using at most concurrency() threads at once.
      • validate

        @Check
        default void validate()