Package org.atlanmod.commons.concurrent
Class MoreExecutors
- java.lang.Object
-
- org.atlanmod.commons.concurrent.MoreExecutors
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ExecutorServicenewDirectPool()Creates a newExecutorServicethat runs each task in the thread that invokesexecute/submit.static ExecutorServicenewFixedThreadPool()Creates a newExecutorServiceusing the maximumavailable processors, that will be closed when the application will exit.static ExecutorServicenewFixedThreadPool(int nThreads)Creates a newExecutorServiceusingnThreadsprocessors, that will be closed when the application will exit.static ExecutorServicenewFixedThreadPool(int nThreads, String name)Creates a newExecutorServiceusingnThreadsprocessors, that will be closed when the application will exit.static ExecutorServicenewFixedThreadPool(String name)Creates a newExecutorServiceusing the maximumavailable processors, that will be closed when the application will exit.static List<Runnable>shutdown(ExecutorService service, long timeout, TimeUnit unit)Cleanly closes theservice.static voidshutdown(ExecutorService service, long timeout, TimeUnit unit, boolean executeUnstarted)Cleanly closes theservice.static ExecutorServiceshutdownAtExit(ExecutorService service, long timeout, TimeUnit unit, boolean executeUnstarted)Adds a shutdown hook to cleanly closes theservicewhen the application will exit.
-
-
-
Method Detail
-
newDirectPool
@Nonnull public static ExecutorService newDirectPool()
Creates a newExecutorServicethat runs each task in the thread that invokesexecute/submit. This applies both to individually submitted tasks and to collections of tasks submitted viainvokeAllorinvokeAny. In the latter case, tasks will run serially on the calling thread. Tasks are run to completion before aFutureis returned to the caller (unless the executor has been shutdown).Although all tasks are immediately executed in the thread that submitted the task, this
ExecutorServiceimposes a small locking overhead on each task submission in order to implement shutdown and termination behavior.- Returns:
- a new service
-
newFixedThreadPool
@Nonnull public static ExecutorService newFixedThreadPool()
Creates a newExecutorServiceusing the maximumavailable processors, that will be closed when the application will exit.- Returns:
- a new service
- Throws:
IllegalArgumentException- ifnThreads <= 0- See Also:
newFixedThreadPool(int)
-
newFixedThreadPool
@Nonnull public static ExecutorService newFixedThreadPool(String name)
Creates a newExecutorServiceusing the maximumavailable processors, that will be closed when the application will exit.- Returns:
- a new service
- Throws:
IllegalArgumentException- ifnThreads <= 0- See Also:
newFixedThreadPool(int)
-
newFixedThreadPool
@Nonnull public static ExecutorService newFixedThreadPool(int nThreads)
Creates a newExecutorServiceusingnThreadsprocessors, that will be closed when the application will exit.- Parameters:
nThreads- the number of threads in the pool- Returns:
- a new service
- Throws:
IllegalArgumentException- ifnThreads <= 0- See Also:
newFixedThreadPool(int, String)
-
newFixedThreadPool
@Nonnull public static ExecutorService newFixedThreadPool(int nThreads, @Nullable String name)
Creates a newExecutorServiceusingnThreadsprocessors, that will be closed when the application will exit.- Parameters:
nThreads- the number of threads in the poolname- the name of the pool- Returns:
- a new service
- Throws:
IllegalArgumentException- ifnThreads <= 0- See Also:
Executors.newFixedThreadPool(int),MoreThreads.newThreadFactory(),shutdownAtExit(ExecutorService, long, TimeUnit, boolean)
-
shutdown
public static void shutdown(ExecutorService service, long timeout, TimeUnit unit, boolean executeUnstarted)
Cleanly closes theservice.- Parameters:
service- the service to closetimeout- the maximum time to waitunit- the time unit of the timeout argumentexecuteUnstarted-trueif the remaining tasks must be executed synchronously if they have not completed their execution after the shutdown request- Throws:
NullPointerException- if any argument isnullIllegalArgumentException- iftimeout < 0- See Also:
ExecutorService.shutdown(),ExecutorService.awaitTermination(long, TimeUnit),ExecutorService.shutdownNow()
-
shutdown
@Nonnull public static List<Runnable> shutdown(ExecutorService service, long timeout, TimeUnit unit)
Cleanly closes theservice.- Parameters:
service- the service to closetimeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- a list of remaining tasks that never commenced execution
- Throws:
NullPointerException- if any argument isnullIllegalArgumentException- iftimeout < 0- See Also:
ExecutorService.shutdown(),ExecutorService.awaitTermination(long, TimeUnit),ExecutorService.shutdownNow()
-
shutdownAtExit
@Nonnull public static ExecutorService shutdownAtExit(ExecutorService service, long timeout, TimeUnit unit, boolean executeUnstarted)
Adds a shutdown hook to cleanly closes theservicewhen the application will exit.- Parameters:
service- the service to closetimeout- the maximum time to waitunit- the time unit of the timeout argumentexecuteUnstarted-trueif the remaining tasks must be executed synchronously if they have not completed their execution after the shutdown request- Returns:
- the
service - Throws:
NullPointerException- if any argument isnullIllegalArgumentException- iftimeout < 0- See Also:
ExecutorService.shutdown(),ExecutorService.awaitTermination(long, TimeUnit),ExecutorService.shutdownNow()
-
-