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.

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static class`
`CommonThreadPool.SameThreadExecutorService`

## Field Summary

Fields
Modifier and Type
Field
Description
`static boolean`
`incorrectPoolUse`
Local variable indicating if there was a thread that was not main, and requested a thread pool

## Constructor Summary

Constructors
Constructor
Description
`CommonThreadPool(ExecutorService pool)`
Constructor of the threadPool.

## Method Summary

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 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)`
`final 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.

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

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

## Field Details

### incorrectPoolUse

public static boolean incorrectPoolUse
Local variable indicating if there was a thread that was not main, and requested a thread pool

## Constructor Details

### 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 Details

### 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 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.

### 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 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`

### 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.