public class ShutdownableThread extends java.lang.Object implements Shutdownable
runnable in a new thread and another shutdown runnable the
graceful shutdown() phase of the thread. The thread is started immediately upon construction.| Modifier | Constructor and Description |
|---|---|
protected |
ShutdownableThread(java.util.function.Function<java.util.function.BooleanSupplier,java.lang.Runnable> mainRunnableFactory,
java.util.function.Function<java.util.function.BooleanSupplier,java.lang.Runnable> shutdownRunnableFactory,
java.util.concurrent.ThreadFactory threadFactory)
Constructor for shutdownable thread; it is recommended to use the static start(..) methods instead.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
awaitTermination(long timeout,
java.util.concurrent.TimeUnit unit)
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs,
whichever happens first.
|
boolean |
isShutdown()
Returns
true if this service has been shut down. |
boolean |
isTerminated()
Returns
true if this service has terminated following shut down. |
void |
shutdown()
Initiates an orderly shutdown in which the service will be continue to perform its duties
as long as there is work to be done.
|
void |
shutdownNow()
Initiates an immediate shutdown in which the service will stop even if there is more work
to be done.
|
static ShutdownableThread |
start(java.util.function.Function<java.util.function.BooleanSupplier,java.lang.Runnable> mainRunnableFactory,
java.util.function.Function<java.util.function.BooleanSupplier,java.lang.Runnable> shutdownRunnableFactory,
java.util.concurrent.ThreadFactory threadFactory)
Creates, starts and returns a new shutdownable thread.
|
java.lang.String |
toString()
Returns the name of the thread that was created with the thread factory passed to the constructor.
|
protected ShutdownableThread(java.util.function.Function<java.util.function.BooleanSupplier,java.lang.Runnable> mainRunnableFactory, java.util.function.Function<java.util.function.BooleanSupplier,java.lang.Runnable> shutdownRunnableFactory, java.util.concurrent.ThreadFactory threadFactory)
mainRunnableFactory - the factory for the main runnable;
the isRunning() condition is passed to the factory as lambdashutdownRunnableFactory - the factory for the shutdown phase runnable;
the isShutdownRunning() condition is passed to the factory as lambdathreadFactory - the factory to provide the threadpublic static ShutdownableThread start(java.util.function.Function<java.util.function.BooleanSupplier,java.lang.Runnable> mainRunnableFactory, java.util.function.Function<java.util.function.BooleanSupplier,java.lang.Runnable> shutdownRunnableFactory, java.util.concurrent.ThreadFactory threadFactory)
mainRunnableFactory - the factory for the main runnable;
the isRunning() condition is passed to the factory as lambdashutdownRunnableFactory - the factory for the shutdown phase runnable;
the isShutdownRunning() condition is passed to the factory as lambdathreadFactory - the factory to provide the threadpublic void shutdown()
Shutdownableperform() methods until all invocations
yield false (indicating that no work was done).
This method does not wait for for termination; use awaitTermination
to do that.
Invocation has no additional effect if already shut down.
shutdown in interface Shutdownablepublic void shutdownNow()
Shutdownableperform() irrespective of prior invocation results.
This method does not wait for for termination; use awaitTermination
to do that.
Invocation has no additional effect if already shut down.
shutdownNow in interface Shutdownablepublic boolean isShutdown()
Shutdownabletrue if this service has been shut down.isShutdown in interface Shutdownabletrue if this service has been shut downpublic boolean isTerminated()
Shutdownabletrue if this service has terminated following shut down. Note that
isTerminated is never true unless either shutdown or
shutdownNow was called first.isTerminated in interface Shutdownabletrue if the service has terminated following shut downpublic boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit unit)
ShutdownableawaitTermination in interface Shutdownabletimeout - the maximum time to waitunit - the time unit of the timeout argumenttrue if this executor terminated and
false if the timeout elapsed before terminationpublic java.lang.String toString()
toString in class java.lang.ObjectThread.getName()