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
`CommonThreadPool​(ExecutorService pool)` Constructor of the threadPool.

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`boolean` `awaitTermination​(long timeout, TimeUnit unit)`
`void` `execute​(Runnable command)`
`static ExecutorService` `get()` Get the shared Executor thread pool, that have the number of threads of the host system
`static 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()`
`static boolean` `isSharedTPThreads​(int k)` Get if there is a current thread pool that have the given parallelism locally.
`boolean` `isShutdown()`
`boolean` `isTerminated()`
`void` `shutdown()`
`static void` `shutdownAsyncPools()` 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)`

### Methods inherited from class java.lang.Object

`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### 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 in
  `tasks` - 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:
  `shutdown` in interface `ExecutorService`

#### shutdownNow

    public List<Runnable> shutdownNow()

Specified by:
  `shutdownNow` in interface `ExecutorService`

#### invokeAll

    public <T> List<Future<T>> invokeAll​(Collection<? extends Callable<T>> tasks)
                                  throws InterruptedException

Specified by:
  `invokeAll` in interface `ExecutorService`
Throws:
  `InterruptedException`

#### invokeAll

    public <T> List<Future<T>> invokeAll​(Collection<? extends Callable<T>> tasks,
                                         long timeout,
                                         TimeUnit unit)
                                  throws InterruptedException

Specified by:
  `invokeAll` in interface `ExecutorService`
Throws:
  `InterruptedException`

#### execute

    public void execute​(Runnable command)

Specified by:
  `execute` in interface `Executor`

#### submit

    public <T> Future<T> submit​(Callable<T> task)

Specified by:
  `submit` in interface `ExecutorService`

#### submit

    public <T> Future<T> submit​(Runnable task,
                                T result)

Specified by:
  `submit` in interface `ExecutorService`

#### submit

    public Future<?> submit​(Runnable task)

Specified by:
  `submit` in interface `ExecutorService`

#### isShutdown

    public boolean isShutdown()

Specified by:
  `isShutdown` in interface `ExecutorService`

#### isTerminated

    public boolean isTerminated()

Specified by:
  `isTerminated` in interface `ExecutorService`

#### awaitTermination

    public boolean awaitTermination​(long timeout,
                                    TimeUnit unit)
                             throws InterruptedException

Specified by:
  `awaitTermination` in interface `ExecutorService`
Throws:
  `InterruptedException`

#### invokeAny

    public <T> T invokeAny​(Collection<? extends Callable<T>> tasks)
                    throws InterruptedException,
                           ExecutionException

Specified by:
  `invokeAny` in interface `ExecutorService`
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 interface `ExecutorService`
Throws:
  `InterruptedException`
  `ExecutionException`
  `TimeoutException`