Class ThreadsPooling.Builder
- java.lang.Object
-
- com.github.javinator9889.threading.pools.ThreadsPooling.Builder
-
- Enclosing class:
- ThreadsPooling
public static final class ThreadsPooling.Builder extends java.lang.Object
Static class for generating a new instance ofThreadsPooling
.This class provides multiple methods for defining a new
ThreadsPooling
instance completely customizable.In addition, for developer fast production, it uses some default values defined so it is possible to just create a new instance of this class just by running:
ThreadsPooling pooling = ThreadsPooling.builder().build();
- Core threads: default core threads.
- Maximum pool size: default maximum pool size.
- Keep alive time: default keep alive time.
- Time unit: default time unit.
- Queue capacity: default queue capacity.
- Rejected execution handler: default rejected execiton handler.
All the methods here allows the developer the ability to totally configure
ThreadPoolExecutor
very easily.
-
-
Field Summary
Fields Modifier and Type Field Description private int
mCoreThreads
private long
mKeepAliveTime
private int
mMaximumPoolSize
private int
mQueueCapacity
private java.util.concurrent.RejectedExecutionHandler
mRejectedExecutionHandler
private java.util.concurrent.ThreadFactory
mThreadFactory
private java.util.concurrent.TimeUnit
mTimeUnit
private java.util.concurrent.BlockingQueue<java.lang.Runnable>
mWorkingThreadsQueue
-
Constructor Summary
Constructors Modifier Constructor Description private
Builder()
Default constructor used atThreadsPooling.builder()
- sets all values to its default one.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ThreadsPooling
build()
Generates a newThreadsPooling
instance by using the provided arguments with the available constructors at that class.private java.lang.IllegalArgumentException
illegalArgumentException(java.lang.String valueName, long valueContent)
Generates anIllegalArgumentException
by replacing the spaces by the given values.private boolean
isParamHigherThanZero(long paramToCheck)
Checks whether the param is higher or equal than zero.private void
setKeepAliveTime(long keepAliveTime)
Sets the keep alive time to the default value or the given one if zero or different.ThreadsPooling.Builder
withConcurrentThreadsRunning(int concurrentThreadsRunning)
Sets the normally amount of concurrent running threads - default is '4'.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.ThreadsPooling.Builder
withKeepAliveInDays(long keepAliveTime)
Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.ThreadsPooling.Builder
withKeepAliveInHours(long keepAliveTime)
Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.ThreadsPooling.Builder
withKeepAliveInMicroseconds(long keepAliveTime)
Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.ThreadsPooling.Builder
withKeepAliveInMilliseconds(long keepAliveTime)
Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.ThreadsPooling.Builder
withKeepAliveInMinutes(long keepAliveTime)
Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.ThreadsPooling.Builder
withKeepAliveInNanoseconds(long keepAliveTime)
Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.ThreadsPooling.Builder
withKeepAliveInSeconds(long keepAliveTime)
Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.ThreadsPooling.Builder
withMaximumPoolSize(int maximumPoolSize)
Sets the maximum amount of concurrent running threads - default is '8'.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.ThreadsPooling.Builder
withQueueCapacity(int queueCapacity)
Sets the queue capacity - default is '100'.ThreadsPooling.Builder
withRejectedExecutionHandler(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 benull
, which will be the default rejected handler.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.ThreadsPooling.Builder
withThread(java.lang.Runnable thread)
Adds a new thread that will be executed by callingThreadsPooling.start()
method.ThreadsPooling.Builder
withThreadFactory(java.util.concurrent.ThreadFactory threadFactory)
Sets the thread factory that will be used for creating threads - can benull
.ThreadsPooling.Builder
withThreads(java.lang.Runnable... threads)
Adds new threads that will be executed by callingThreadsPooling.start()
method.ThreadsPooling.Builder
withTimeUnit(java.util.concurrent.TimeUnit timeUnit)
Sets the keep alive time unit - default is milliseconds.
-
-
-
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
-
Builder
private Builder()
Default constructor used atThreadsPooling.builder()
- sets all values to its default one.
-
-
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 theparam
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 theparam
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 theparam
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 theparam
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 theparam
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 theparam
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 theparam
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 theparam
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 theparam
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 theparam
is lower than zero.
-
withThread
public ThreadsPooling.Builder withThread(@NotNull java.lang.Runnable thread)
Adds a new thread that will be executed by callingThreadsPooling.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 callingThreadsPooling.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 benull
.- 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 benull
, 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 newThreadsPooling
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, elsefalse
.
-
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 anIllegalArgumentException
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.
-
-