Package org.apache.sysds.runtime.util
Class CommonThreadPool
- java.lang.Object
-
- org.apache.sysds.runtime.util.CommonThreadPool
-
- All Implemented Interfaces:
Executor,ExecutorService
public class CommonThreadPool extends Object implements ExecutorService
This common thread pool provides an abstraction to obtain a shared thread pool. If the number of logical cores is specified a ForkJoinPool.commonPool is returned on all requests. If pools of different size are requested, we create new pool instances of FixedThreadPool, Unless we currently are on the main thread, Then we return a shared instance of the first requested number of cores. Alternatively the class also contain a dynamic threadPool, that is intended for asynchronous long running tasks with low compute overhead, such as broadcast and collect from federated workers.
-
-
Constructor Summary
Constructors Constructor Description CommonThreadPool(ExecutorService pool)Constructor of the threadPool.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanawaitTermination(long timeout, TimeUnit unit)voidexecute(Runnable command)static ExecutorServiceget()Get the shared Executor thread pool, that have the number of threads of the host systemstatic ExecutorServiceget(int k)Get a Executor thread pool, that have the number of threads specified in k.static ExecutorServicegetDynamicPool()Get a dynamic thread pool that allocate threads as the requests are made.<T> List<Future<T>>invokeAll(Collection<? extends Callable<T>> tasks)<T> List<Future<T>>invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)static <T> voidinvokeAndShutdown(ExecutorService pool, Collection<? extends Callable<T>> tasks)Invoke the collection of tasks and shutdown the pool upon job termination.<T> TinvokeAny(Collection<? extends Callable<T>> tasks)<T> TinvokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)booleanisCached()static booleanisSharedTPThreads(int k)Get if there is a current thread pool that have the given parallelism locally.booleanisShutdown()booleanisTerminated()voidshutdown()static voidshutdownAsyncPools()Shutdown the cached thread pools.List<Runnable>shutdownNow()Future<?>submit(Runnable task)<T> Future<T>submit(Runnable task, T result)<T> Future<T>submit(Callable<T> task)
-
-
-
Constructor Detail
-
CommonThreadPool
public CommonThreadPool(ExecutorService pool)
Constructor of the threadPool. This is intended not to be used except for tests. Please use the static constructors.- Parameters:
pool- The thread pool instance to use.
-
-
Method Detail
-
get
public static ExecutorService get()
Get the shared Executor thread pool, that have the number of threads of the host system- Returns:
- An ExecutorService
-
get
public static ExecutorService get(int k)
Get a Executor thread pool, that have the number of threads specified in k. The thread pool can be reused by other processes in the same host thread requesting another pool of the same number of threads. The executor that is guaranteed ThreadLocal except if it is number of host logical cores.- Parameters:
k- The number of threads wanted- Returns:
- The executor with specified parallelism
-
isSharedTPThreads
public static boolean isSharedTPThreads(int k)
Get if there is a current thread pool that have the given parallelism locally.- Parameters:
k- the parallelism- Returns:
- If we have a cached thread pool.
-
invokeAndShutdown
public static <T> void invokeAndShutdown(ExecutorService pool, Collection<? extends Callable<T>> tasks)
Invoke the collection of tasks and shutdown the pool upon job termination.- Type Parameters:
T- The type of class to return from the job- Parameters:
pool- The pool to execute intasks- The tasks to execute
-
getDynamicPool
public static ExecutorService getDynamicPool()
Get a dynamic thread pool that allocate threads as the requests are made. This pool is intended for async remote calls that does not depend on local compute.- Returns:
- A dynamic thread pool.
-
shutdownAsyncPools
public static void shutdownAsyncPools()
Shutdown the cached thread pools.
-
isCached
public final boolean isCached()
-
shutdown
public void shutdown()
- Specified by:
shutdownin interfaceExecutorService
-
shutdownNow
public List<Runnable> shutdownNow()
- Specified by:
shutdownNowin interfaceExecutorService
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
- Specified by:
invokeAllin interfaceExecutorService- Throws:
InterruptedException
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
- Specified by:
invokeAllin interfaceExecutorService- Throws:
InterruptedException
-
submit
public <T> Future<T> submit(Callable<T> task)
- Specified by:
submitin interfaceExecutorService
-
submit
public <T> Future<T> submit(Runnable task, T result)
- Specified by:
submitin interfaceExecutorService
-
submit
public Future<?> submit(Runnable task)
- Specified by:
submitin interfaceExecutorService
-
isShutdown
public boolean isShutdown()
- Specified by:
isShutdownin interfaceExecutorService
-
isTerminated
public boolean isTerminated()
- Specified by:
isTerminatedin interfaceExecutorService
-
awaitTermination
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException- Specified by:
awaitTerminationin interfaceExecutorService- Throws:
InterruptedException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
- Specified by:
invokeAnyin interfaceExecutorService- Throws:
InterruptedExceptionExecutionException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
- Specified by:
invokeAnyin interfaceExecutorService- Throws:
InterruptedExceptionExecutionExceptionTimeoutException
-
-