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.ObjectStatic class for generating a new instance ofThreadsPooling.This class provides multiple methods for defining a new
ThreadsPoolinginstance 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:
The default values are: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
ThreadPoolExecutorvery easily.
-
-
Field Summary
Fields Modifier and Type Field Description private intmCoreThreadsprivate longmKeepAliveTimeprivate intmMaximumPoolSizeprivate intmQueueCapacityprivate java.util.concurrent.RejectedExecutionHandlermRejectedExecutionHandlerprivate java.util.concurrent.ThreadFactorymThreadFactoryprivate java.util.concurrent.TimeUnitmTimeUnitprivate java.util.concurrent.BlockingQueue<java.lang.Runnable>mWorkingThreadsQueue
-
Constructor Summary
Constructors Modifier Constructor Description privateBuilder()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 ThreadsPoolingbuild()Generates a newThreadsPoolinginstance by using the provided arguments with the available constructors at that class.private java.lang.IllegalArgumentExceptionillegalArgumentException(java.lang.String valueName, long valueContent)Generates anIllegalArgumentExceptionby replacing the spaces by the given values.private booleanisParamHigherThanZero(long paramToCheck)Checks whether the param is higher or equal than zero.private voidsetKeepAliveTime(long keepAliveTime)Sets the keep alive time to the default value or the given one if zero or different.ThreadsPooling.BuilderwithConcurrentThreadsRunning(int concurrentThreadsRunning)Sets the normally amount of concurrent running threads - default is '4'.ThreadsPooling.BuilderwithImmediatelyRunRejectedTask()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.BuilderwithKeepAliveInDays(long keepAliveTime)Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.ThreadsPooling.BuilderwithKeepAliveInHours(long keepAliveTime)Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.ThreadsPooling.BuilderwithKeepAliveInMicroseconds(long keepAliveTime)Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.ThreadsPooling.BuilderwithKeepAliveInMilliseconds(long keepAliveTime)Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.ThreadsPooling.BuilderwithKeepAliveInMinutes(long keepAliveTime)Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.ThreadsPooling.BuilderwithKeepAliveInNanoseconds(long keepAliveTime)Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.ThreadsPooling.BuilderwithKeepAliveInSeconds(long keepAliveTime)Sets the keep alive time, which is the amount of time that threads may remain idle before being terminated.ThreadsPooling.BuilderwithMaximumPoolSize(int maximumPoolSize)Sets the maximum amount of concurrent running threads - default is '8'.ThreadsPooling.BuilderwithNoActionOnTaskRejected()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.BuilderwithQueueCapacity(int queueCapacity)Sets the queue capacity - default is '100'.ThreadsPooling.BuilderwithRejectedExecutionHandler(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.BuilderwithRunningTaskWhenAllCompleted()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.BuilderwithThread(java.lang.Runnable thread)Adds a new thread that will be executed by callingThreadsPooling.start()method.ThreadsPooling.BuilderwithThreadFactory(java.util.concurrent.ThreadFactory threadFactory)Sets the thread factory that will be used for creating threads - can benull.ThreadsPooling.BuilderwithThreads(java.lang.Runnable... threads)Adds new threads that will be executed by callingThreadsPooling.start()method.ThreadsPooling.BuilderwithTimeUnit(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:
Builderitself.- Throws:
java.lang.IllegalArgumentException- when theparamis 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:
Builderitself.- Throws:
java.lang.IllegalArgumentException- when theparamis 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:
Builderitself.- Throws:
java.lang.IllegalArgumentException- when theparamis 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:
Builderitself.- Throws:
java.lang.IllegalArgumentException- when theparamis 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:
Builderitself.- Throws:
java.lang.IllegalArgumentException- when theparamis 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:
Builderitself.- Throws:
java.lang.IllegalArgumentException- when theparamis 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:
Builderitself.- Throws:
java.lang.IllegalArgumentException- when theparamis 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:
Builderitself.- Throws:
java.lang.IllegalArgumentException- when theparamis 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:
Builderitself.- Throws:
java.lang.IllegalArgumentException- when theparamis 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:
Builderitself.
-
withQueueCapacity
public ThreadsPooling.Builder withQueueCapacity(int queueCapacity)
Sets the queue capacity - default is '100'.- Parameters:
queueCapacity- new queue capacity.- Returns:
Builderitself.- Throws:
java.lang.IllegalArgumentException- when theparamis 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:
Builderitself.- 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:
Builderitself.- 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:
Builderitself.
-
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:
Builderitself.
-
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:
Builderitself.
-
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:
Builderitself.
-
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:
Builderitself.
-
build
public ThreadsPooling build()
Generates a newThreadsPoolinginstance by using the provided arguments with the available constructors at that class.- Returns:
ThreadsPoolingnew instance.
-
isParamHigherThanZero
private boolean isParamHigherThanZero(long paramToCheck)
Checks whether the param is higher or equal than zero.- Parameters:
paramToCheck- value to check.- Returns:
trueif 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 anIllegalArgumentExceptionby replacing the spaces by the given values.- Parameters:
valueName- name of the value that is not correct.valueContent- value that is not correct.- Returns:
IllegalArgumentExceptionconfigured.
-
-