Class ThreadsPooling.Builder

    • Field Detail

      • mCoreThreads

        private int mCoreThreads
      • mMaximumPoolSize

        private int mMaximumPoolSize
      • mKeepAliveTime

        private long mKeepAliveTime
      • mTimeUnit

        private java.util.concurrent.TimeUnit mTimeUnit
      • mQueueCapacity

        private int mQueueCapacity
      • mWorkingThreadsQueue

        private java.util.concurrent.BlockingQueue<java.lang.Runnable> mWorkingThreadsQueue
      • mThreadFactory

        private java.util.concurrent.ThreadFactory mThreadFactory
      • mRejectedExecutionHandler

        private java.util.concurrent.RejectedExecutionHandler mRejectedExecutionHandler
    • Constructor Detail

    • Method Detail

      • withConcurrentThreadsRunning

        public ThreadsPooling.Builder withConcurrentThreadsRunning​(int concurrentThreadsRunning)
        Sets the normally amount of concurrent running threads - default is '4'.
        Parameters:
        concurrentThreadsRunning - amount of concurrent running threads.
        Returns:
        Builder itself.
        Throws:
        java.lang.IllegalArgumentException - when the param is lower than zero.
      • withMaximumPoolSize

        public ThreadsPooling.Builder withMaximumPoolSize​(int maximumPoolSize)
        Sets the maximum amount of concurrent running threads - default is '8'.
        Parameters:
        maximumPoolSize - amount of maximum concurrent running threads.
        Returns:
        Builder itself.
        Throws:
        java.lang.IllegalArgumentException - when the param is lower than zero.
      • withKeepAliveInSeconds

        public ThreadsPooling.Builder withKeepAliveInSeconds​(long keepAliveTime)
        Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.
        Parameters:
        keepAliveTime - time in seconds.
        Returns:
        Builder itself.
        Throws:
        java.lang.IllegalArgumentException - when the param is lower than zero.
      • withKeepAliveInMilliseconds

        public ThreadsPooling.Builder withKeepAliveInMilliseconds​(long keepAliveTime)
        Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.
        Parameters:
        keepAliveTime - time in milliseconds.
        Returns:
        Builder itself.
        Throws:
        java.lang.IllegalArgumentException - when the param is lower than zero.
      • withKeepAliveInMicroseconds

        public ThreadsPooling.Builder withKeepAliveInMicroseconds​(long keepAliveTime)
        Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.
        Parameters:
        keepAliveTime - time in microseconds.
        Returns:
        Builder itself.
        Throws:
        java.lang.IllegalArgumentException - when the param is lower than zero.
      • withKeepAliveInNanoseconds

        public ThreadsPooling.Builder withKeepAliveInNanoseconds​(long keepAliveTime)
        Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.
        Parameters:
        keepAliveTime - time in nanoseconds.
        Returns:
        Builder itself.
        Throws:
        java.lang.IllegalArgumentException - when the param is lower than zero.
      • withKeepAliveInMinutes

        public ThreadsPooling.Builder withKeepAliveInMinutes​(long keepAliveTime)
        Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.
        Parameters:
        keepAliveTime - time in minutes.
        Returns:
        Builder itself.
        Throws:
        java.lang.IllegalArgumentException - when the param is lower than zero.
      • withKeepAliveInHours

        public ThreadsPooling.Builder withKeepAliveInHours​(long keepAliveTime)
        Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.
        Parameters:
        keepAliveTime - time in hours.
        Returns:
        Builder itself.
        Throws:
        java.lang.IllegalArgumentException - when the param is lower than zero.
      • withKeepAliveInDays

        public ThreadsPooling.Builder withKeepAliveInDays​(long keepAliveTime)
        Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.
        Parameters:
        keepAliveTime - time in days.
        Returns:
        Builder itself.
        Throws:
        java.lang.IllegalArgumentException - when the param is lower than zero.
      • withTimeUnit

        public ThreadsPooling.Builder withTimeUnit​(@Nullable
                                                   java.util.concurrent.TimeUnit timeUnit)
        Sets the keep alive time unit - default is milliseconds.
        Parameters:
        timeUnit - keep alive time unit.
        Returns:
        Builder itself.
      • withQueueCapacity

        public ThreadsPooling.Builder withQueueCapacity​(int queueCapacity)
        Sets the queue capacity - default is '100'.
        Parameters:
        queueCapacity - new queue capacity.
        Returns:
        Builder itself.
        Throws:
        java.lang.IllegalArgumentException - when the param is lower than zero.
      • withThread

        public ThreadsPooling.Builder withThread​(@NotNull
                                                 java.lang.Runnable thread)
        Adds a new thread that will be executed by calling ThreadsPooling.start() method.
        Parameters:
        thread - thread that will be executed.
        Returns:
        Builder itself.
        Throws:
        java.lang.IllegalStateException - if the element cannot be added at this time due to capacity restrictions.
      • withThreads

        public ThreadsPooling.Builder withThreads​(@NotNull
                                                  java.lang.Runnable... threads)
        Adds new threads that will be executed by calling ThreadsPooling.start() method.
        Parameters:
        threads - thread that will be executed.
        Returns:
        Builder itself.
        Throws:
        java.lang.IllegalStateException - if the element cannot be added at this time due to capacity restrictions.
      • withThreadFactory

        public ThreadsPooling.Builder withThreadFactory​(@Nullable
                                                        java.util.concurrent.ThreadFactory threadFactory)
        Sets the thread factory that will be used for creating threads - can be null.
        Parameters:
        threadFactory - thread factory that will be used.
        Returns:
        Builder itself.
      • withRejectedExecutionHandler

        public ThreadsPooling.Builder withRejectedExecutionHandler​(@Nullable
                                                                   java.util.concurrent.RejectedExecutionHandler handler)
        Sets the rejected execution handler that will be used when a new thread cannot be included inside the threads queue (due to capacity restrictions) - can be null , which will be the default rejected handler.
        Parameters:
        handler - rejected execution handler.
        Returns:
        Builder itself.
      • withNoActionOnTaskRejected

        public ThreadsPooling.Builder withNoActionOnTaskRejected()
        Sets the rejected execution handler that will be used when a new thread cannot be included inside the threads queue (due to capacity restrictions) that does nothing.
        Returns:
        Builder itself.
      • withImmediatelyRunRejectedTask

        public ThreadsPooling.Builder withImmediatelyRunRejectedTask()
        Sets the rejected execution handler that will be used when a new thread cannot be included inside the threads queue (due to capacity restrictions) that immediately runs the rejected thread.
        Returns:
        Builder itself.
      • withRunningTaskWhenAllCompleted

        public ThreadsPooling.Builder withRunningTaskWhenAllCompleted()
        Sets the rejected execution handler that will be used when a new thread cannot be included inside the threads queue (due to capacity restrictions) that runs the rejected thread after requesting the shutdown of the executor.
        Returns:
        Builder itself.
      • build

        public ThreadsPooling build()
        Generates a new ThreadsPooling instance by using the provided arguments with the available constructors at that class.
        Returns:
        ThreadsPooling new instance.
      • isParamHigherThanZero

        private boolean isParamHigherThanZero​(long paramToCheck)
        Checks whether the param is higher or equal than zero.
        Parameters:
        paramToCheck - value to check.
        Returns:
        true if zero or higher, else false.
      • setKeepAliveTime

        private void setKeepAliveTime​(long keepAliveTime)
        Sets the keep alive time to the default value or the given one if zero or different.
        Parameters:
        keepAliveTime - possible keep alive time to set-up.
      • illegalArgumentException

        private java.lang.IllegalArgumentException illegalArgumentException​(java.lang.String valueName,
                                                                            long valueContent)
        Generates an IllegalArgumentException by replacing the spaces by the given values.
        Parameters:
        valueName - name of the value that is not correct.
        valueContent - value that is not correct.
        Returns:
        IllegalArgumentException configured.