|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.google.common.util.concurrent.MoreExecutors
@Beta public final class MoreExecutors
Factory and utility methods for Executor
, ExecutorService
, and ThreadFactory
.
Method Summary | |
---|---|
static void |
addDelayedShutdownHook(ExecutorService service,
long terminationTimeout,
TimeUnit timeUnit)
Add a shutdown hook to wait for thread completion in the given service . |
static ExecutorService |
getExitingExecutorService(ThreadPoolExecutor executor)
Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application is complete. |
static ExecutorService |
getExitingExecutorService(ThreadPoolExecutor executor,
long terminationTimeout,
TimeUnit timeUnit)
Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application is complete. |
static ScheduledExecutorService |
getExitingScheduledExecutorService(ScheduledThreadPoolExecutor executor)
Converts the given ThreadPoolExecutor into a ScheduledExecutorService that exits when the application is complete. |
static ScheduledExecutorService |
getExitingScheduledExecutorService(ScheduledThreadPoolExecutor executor,
long terminationTimeout,
TimeUnit timeUnit)
Converts the given ScheduledThreadPoolExecutor into a ScheduledExecutorService that exits when the application is complete. |
static ExecutorService |
sameThreadExecutor()
Creates an executor service that runs each task in the thread that invokes execute/submit , as in ThreadPoolExecutor.CallerRunsPolicy This
applies both to individually submitted tasks and to collections of tasks
submitted via invokeAll or invokeAny . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static ExecutorService getExitingExecutorService(ThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit)
This is mainly for fixed thread pools.
See Executors.newFixedThreadPool(int)
.
executor
- the executor to modify to make sure it exits when the
application is finishedterminationTimeout
- how long to wait for the executor to
finish before terminating the JVMtimeUnit
- unit of time for the time parameter
public static ScheduledExecutorService getExitingScheduledExecutorService(ScheduledThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit)
This is mainly for fixed thread pools.
See Executors.newScheduledThreadPool(int)
.
executor
- the executor to modify to make sure it exits when the
application is finishedterminationTimeout
- how long to wait for the executor to
finish before terminating the JVMtimeUnit
- unit of time for the time parameter
public static void addDelayedShutdownHook(ExecutorService service, long terminationTimeout, TimeUnit timeUnit)
service
. This is useful if the given service uses
daemon threads, and we want to keep the JVM from exiting immediately on
shutdown, instead giving these daemon threads a chance to terminate
normally.
service
- ExecutorService which uses daemon threadsterminationTimeout
- how long to wait for the executor to finish
before terminating the JVMtimeUnit
- unit of time for the time parameterpublic static ExecutorService getExitingExecutorService(ThreadPoolExecutor executor)
This method waits 120 seconds before continuing with JVM termination, even if the executor has not finished its work.
This is mainly for fixed thread pools.
See Executors.newFixedThreadPool(int)
.
executor
- the executor to modify to make sure it exits when the
application is finished
public static ScheduledExecutorService getExitingScheduledExecutorService(ScheduledThreadPoolExecutor executor)
This method waits 120 seconds before continuing with JVM termination, even if the executor has not finished its work.
This is mainly for fixed thread pools.
See Executors.newScheduledThreadPool(int)
.
executor
- the executor to modify to make sure it exits when the
application is finished
public static ExecutorService sameThreadExecutor()
execute/submit
, as in ThreadPoolExecutor.CallerRunsPolicy
This
applies both to individually submitted tasks and to collections of tasks
submitted via invokeAll
or invokeAny
. In the latter case,
tasks will run serially on the calling thread. Tasks are run to
completion before a Future
is returned to the caller (unless the
executor has been shutdown).
Although all tasks are immediately executed in the thread that
submitted the task, this ExecutorService
imposes a small
locking overhead on each task submission in order to implement shutdown
and termination behavior.
The implementation deviates from the ExecutorService
specification with regards to the shutdownNow
method. First,
"best-effort" with regards to canceling running tasks is implemented
as "no-effort". No interrupts or other attempts are made to stop
threads executing tasks. Second, the returned list will always be empty,
as any submitted task is considered to have started execution.
This applies also to tasks given to invokeAll
or invokeAny
which are pending serial execution, even the subset of the tasks that
have not yet started execution. It is unclear from the
ExecutorService
specification if these should be included, and
it's much easier to implement the interpretation that they not be.
Finally, a call to shutdown
or shutdownNow
may result
in concurrent calls to invokeAll/invokeAny
throwing
RejectedExecutionException, although a subset of the tasks may already
have been executed.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |