Class GThreadPool
The simple behavior for when new tasks are submitted:
1) If there any idle threads, use that thread.
2) If all existing threads are busy and the number of threads is less than max threads, add a
new thread and use it.
3) if all threads are busy and there are max number of threads, queue the item until a thread
becomes free.
The simple behavior for when tasks are completed by a thread:
1) If there are tasks in the queue, start processing a new item in the newly freed thread.
2) if there are more threads that min threads, allow this thread to die if no new
jobs arrive before
the "KEEP ALIVE" time expires which is currently 15 seconds.
3) if there are min threads or less, allow this thread to wait forever for a new job
to arrive.
-
Method Summary
Modifier and TypeMethodDescriptionReturns theExecutor
used by this thread pool.int
Returns the maximum number of threads to use in this thread pool.int
Returns the minimum number of threads to keep alive in this thread pool.static GThreadPool
getPrivateThreadPool
(String name) Creates a new, private thread pool with the given name.static GThreadPool
getSharedThreadPool
(String name) Returns a shared GThreadPool.boolean
Returns true if this is not a shared thread pool.static CompletableFuture
<Void> Runs the given runnable in a background thread using a shared thread pool of the given name.void
setMaxThreadCount
(int maxThreadCount) Sets the max number of threads to use in this thread pool.void
setMinThreadCount
(int minThreadCount) Sets the minimum number of threads to keep alive in this thread pool.void
Future
<?> Submits a runnable to be executed by this thread pool.<T> Future
<T> Submits a runnable to be executed by this thread pool.<T> Future
<T> Submits a callable to be executed by this thread pool.void
submit
(FutureTask<?> futureTask) Submits a FutreTask to be executed by a thread in this thread pool.
-
Method Details
-
getPrivateThreadPool
Creates a new, private thread pool with the given name.- Parameters:
name
- the name of the thread pool- Returns:
- a private GThreadPool with the given name.
-
runAsync
Runs the given runnable in a background thread using a shared thread pool of the given name.- Parameters:
poolName
- the thread pool namer
- the runnable- Returns:
- the future
-
setMaxThreadCount
public void setMaxThreadCount(int maxThreadCount) Sets the max number of threads to use in this thread pool. The default is the number of processors + 1.- Parameters:
maxThreadCount
- the maximum number of threads to use in this thread pool.
-
getMinThreadCount
public int getMinThreadCount()Returns the minimum number of threads to keep alive in this thread pool.- Returns:
- the minimum number of threads to keep alive in this thread pool.
-
setMinThreadCount
public void setMinThreadCount(int minThreadCount) Sets the minimum number of threads to keep alive in this thread pool.- Parameters:
minThreadCount
- the minimum number of threads to keep alive in this thread pool.
-
getMaxThreadCount
public int getMaxThreadCount()Returns the maximum number of threads to use in this thread pool.- Returns:
- the maximum number of threads to use in this thread pool.
-
submit
Submits a FutreTask to be executed by a thread in this thread pool.- Parameters:
futureTask
- the future task to be executed.
-
submit
Submits a runnable to be executed by this thread pool.- Parameters:
task
- the runnable to be executed.- Returns:
- a Future for that runnable.
-
submit
Submits a runnable to be executed by this thread pool.- Parameters:
task
- the runnable to be executed.result
- the result to be returned after the runnable has executed.- Returns:
- a Future for that runnable.
-
submit
Submits a callable to be executed by this thread pool.- Parameters:
task
- the callable to be executed.- Returns:
- a Future for that callable.
-
shutdownNow
public void shutdownNow() -
isPrivate
public boolean isPrivate()Returns true if this is not a shared thread pool.- Returns:
- true if this is not a shared thread pool.
-
getExecutor
Returns theExecutor
used by this thread pool.Note: normal usage of this thread pool contraindicates accessing the executor of this pool. For managing your own jobs, you should use the method on this class directly. The intent of this method is to provide access to the executor so that it may be passed to other asynchronous APIs, such as the
CompletableFuture
.- Returns:
- the executor
-