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 boolean
awaitTermination(long timeout, TimeUnit unit)
void
execute(Runnable command)
static boolean
generalCached()
Get if the current threads that calls have a cached thread pool available.static ExecutorService
get()
Get the shared Executor thread pool, that have the number of threads of the host systemstatic ExecutorService
get(int k)
Get a Executor thread pool, that have the number of threads specified in k.static ExecutorService
getDynamicPool()
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> void
invokeAndShutdown(ExecutorService pool, Collection<? extends Callable<T>> tasks)
Invoke the collection of tasks and shutdown the pool upon job termination.<T> T
invokeAny(Collection<? extends Callable<T>> tasks)
<T> T
invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
boolean
isCached()
If there is a thread pool cached for this thread.boolean
isShutdown()
boolean
isTerminated()
void
shutdown()
static void
shutdownAsyncPools()
Shutdown the cached thread pools.static void
shutdownAsyncPools(Thread thread)
Shutdown the asynchronous pool associated with the given thread.List<Runnable>
shutdownNow()
Future<?>
submit(Runnable task)
<T> Future<T>
submit(Runnable task, T result)
<T> Future<T>
submit(Callable<T> task)
static boolean
useParallelismOnThread()
This method returns true or false depending on if the current thread have an overhead of having to allocate it's own thread pool if parallelization is requested.
-
-
-
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
-
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.
-
shutdownAsyncPools
public static void shutdownAsyncPools(Thread thread)
Shutdown the asynchronous pool associated with the given thread.- Parameters:
thread
- The thread given that could or could not have allocated a thread pool itself.
-
generalCached
public static boolean generalCached()
Get if the current threads that calls have a cached thread pool available. This method is synchronized to analyze the general cache not the common thread pool for the main thread.- Returns:
- If there is a thread pool allocated for this thread.
-
isCached
public final boolean isCached()
If there is a thread pool cached for this thread.- Returns:
- if there is a cached thread pool.
-
shutdown
public void shutdown()
- Specified by:
shutdown
in interfaceExecutorService
-
shutdownNow
public List<Runnable> shutdownNow()
- Specified by:
shutdownNow
in interfaceExecutorService
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
- Specified by:
invokeAll
in interfaceExecutorService
- Throws:
InterruptedException
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
- Specified by:
invokeAll
in interfaceExecutorService
- Throws:
InterruptedException
-
submit
public <T> Future<T> submit(Callable<T> task)
- Specified by:
submit
in interfaceExecutorService
-
submit
public <T> Future<T> submit(Runnable task, T result)
- Specified by:
submit
in interfaceExecutorService
-
submit
public Future<?> submit(Runnable task)
- Specified by:
submit
in interfaceExecutorService
-
isShutdown
public boolean isShutdown()
- Specified by:
isShutdown
in interfaceExecutorService
-
isTerminated
public boolean isTerminated()
- Specified by:
isTerminated
in interfaceExecutorService
-
awaitTermination
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
- Specified by:
awaitTermination
in interfaceExecutorService
- Throws:
InterruptedException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
- Specified by:
invokeAny
in interfaceExecutorService
- Throws:
InterruptedException
ExecutionException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
- Specified by:
invokeAny
in interfaceExecutorService
- Throws:
InterruptedException
ExecutionException
TimeoutException
-
useParallelismOnThread
public static boolean useParallelismOnThread()
This method returns true or false depending on if the current thread have an overhead of having to allocate it's own thread pool if parallelization is requested. Currently if it is the Main thread or a PARFOR thread, then we suggest using parallelism since we can reuse the allocated threads.- Returns:
- If parallelism is suggested.
-
-