public class ThreadBuilder
extends java.lang.Object
The thread will be active only until the timeout be fired.
The thread will be active only after the time delay be completed.
The thread will be repeated after the time interval be completed.
Handles of uncaught exceptions can be thrown and handled within threads.
MINIMAL_REQUIRED_DELAY) if and only if a
(setAfterExecuteConsumer(BiConsumer))
or a (setUncaughtExceptionConsumer(Consumer))
is present.
Example:
final ExecutorService thread = ThreadBuilder
.newBuilder() //New object to build a new thread.
.setDelay(1000) //The thread will wait one second before start.
.setTimeout(4000) //The thread will be canceled after four seconds.
.setInterval(1000) //The thread will be repeated every second.
.setAfterExecuteConsumer(afterExecuteConsumer) //A consumer will be called after thread execution.
.setUncaughtExceptionConsumer(throwableConsumer) //A consumer will be called after any exception thrown.
.setMayInterruptIfRunning(true) //The thread interruption/cancellation will not wait execution.
.setSilentInterruption(true) //Interruption and Cancellation exceptions will not be thrown.
.setExecution(anyRunnable) //The thread execution.
.setThreadNameSupplier(() -> "Thread name")
.setThreadPrioritySupplier(() -> 4)
.start();
ScheduledCaughtExecutorService| Modifier and Type | Field and Description |
|---|---|
static long |
MINIMAL_REQUIRED_DELAY
1000 milliseconds as a minimal delay.
|
| Modifier and Type | Method and Description |
|---|---|
static ThreadBuilder |
newBuilder() |
static ThreadBuilder |
newBuilder(int corePoolSize) |
ThreadBuilder |
setAfterExecuteConsumer(java.util.function.BiConsumer<java.lang.Runnable,java.lang.Throwable> afterExecuteConsumer)
Sets the consumer to be called after thread execution.
|
ThreadBuilder |
setDelay(long milliseconds)
Sets the delay value.
|
ThreadBuilder |
setExecution(java.lang.Runnable execution)
Sets the thread execution.
|
ThreadBuilder |
setInterval(long milliseconds)
Sets the repeating interval value.
|
ThreadBuilder |
setMayInterruptIfRunning(boolean flag)
Sets the thread-interrupting-flag.
|
ThreadBuilder |
setSilentInterruption(boolean flag)
Sets the thread-silent-interrupting-flag.
|
ThreadBuilder |
setThreadNameSupplier(java.util.function.Supplier<java.lang.String> threadNameSupplier)
Sets the thread name supplier.The thread factory will consume this supplier to generate a thread name
before return a new thread.
|
ThreadBuilder |
setThreadPrioritySupplier(java.util.function.Supplier<java.lang.Integer> threadPrioritySupplier)
Sets the thread priority supplier.The thread factory will consume this supplier to generate a thread priority
before return a new thread.
|
ThreadBuilder |
setTimeout(long milliseconds)
Sets the timeout value.
|
ThreadBuilder |
setUncaughtExceptionConsumer(java.util.function.Consumer<java.lang.Throwable> uncaughtExceptionConsumer)
Sets the consumer to be called after exception throwing.
|
ExecutorResult |
start()
Starts the thread.
|
ThreadBuilder |
startAndBuildOther()
Starts the thread.
|
public static final long MINIMAL_REQUIRED_DELAY
public static ThreadBuilder newBuilder()
public static ThreadBuilder newBuilder(int corePoolSize)
corePoolSize - the ScheduledCaughtExecutorService pool size.ScheduledCaughtExecutorService.ScheduledCaughtExecutorService(int)public ThreadBuilder setTimeout(long milliseconds)
milliseconds - the timeout value in milliseconds.public ThreadBuilder setDelay(long milliseconds)
milliseconds - the delay value in milliseconds.public ThreadBuilder setInterval(long milliseconds)
milliseconds - the repeating interval value in milliseconds.public ThreadBuilder setAfterExecuteConsumer(java.util.function.BiConsumer<java.lang.Runnable,java.lang.Throwable> afterExecuteConsumer)
afterExecuteConsumer - the consumer to be called after thread execution.ScheduledCaughtExecutorService.afterExecute(Runnable, Throwable)public ThreadBuilder setUncaughtExceptionConsumer(java.util.function.Consumer<java.lang.Throwable> uncaughtExceptionConsumer)
uncaughtExceptionConsumer - the consumer to be called after exception throwing.public ThreadBuilder setThreadNameSupplier(java.util.function.Supplier<java.lang.String> threadNameSupplier)
threadNameSupplier - the thread name supplier.public ThreadBuilder setThreadPrioritySupplier(java.util.function.Supplier<java.lang.Integer> threadPrioritySupplier)
threadPrioritySupplier - the thread priority supplier.public ThreadBuilder setExecution(java.lang.Runnable execution)
execution - the thread execution(Runnable)public ThreadBuilder setMayInterruptIfRunning(boolean flag)
flag - true if the thread executing this task should be interrupted;
otherwise, in-progress tasks are allowed to complete.Future.cancel(boolean)public ThreadBuilder setSilentInterruption(boolean flag)
flag - true if the Interruption/Cancellation exceptions should be ignored.Future.cancel(boolean),
CancellationException,
InterruptedExceptionpublic ExecutorResult start()
ExecutorResultpublic ThreadBuilder startAndBuildOther()