public class ThreadGroupPool extends Object
ThreadGroupPool is a very simple thread pool where each
pooled thread is in its own ThreadGroup. Unfortunately
threads cannot be moved around between thread groups, so we just
pool (ThreadGroup, Thread) pairs. If additional threads are
started in a thread group, they are expected to have exited before
the runnable provided to start(java.lang.String, java.lang.Runnable) completes. If this is not
the case, the thread will be dropped from the thread pool and
detailed diagnostics will be written to the log.
Unlike thread names, thread group names are immutable so thread
groups will be named with a specified prefix with a counter
appended. The name of the main thread for each thread pool is
determined when start(java.lang.String, java.lang.Runnable) is called.
| Modifier and Type | Class and Description |
|---|---|
static class |
ThreadGroupPool.Builder
Builder for ThreadGroupPool.
|
| Constructor and Description |
|---|
ThreadGroupPool(ThreadGroup parentThreadGroup,
String threadGroupNamePrefix,
Thread.UncaughtExceptionHandler uncaughtExceptionHandler,
boolean ignoreDaemonThreads) |
| Modifier and Type | Method and Description |
|---|---|
static ThreadGroupPool.Builder |
builder() |
static CountDownLatch |
resetCurrentThread()
If the current thread is main thread started in response to a
call to
start(java.lang.String, java.lang.Runnable), this method will arrange for it to expect
to be "restarted." See RestartableThread for more
information. |
void |
start(String threadName,
Runnable runnable)
Execute
runnable in a thread named threadName. |
int |
waitingThreadCount() |
public ThreadGroupPool(ThreadGroup parentThreadGroup, String threadGroupNamePrefix, Thread.UncaughtExceptionHandler uncaughtExceptionHandler, boolean ignoreDaemonThreads)
public static ThreadGroupPool.Builder builder()
public void start(String threadName, Runnable runnable) throws InterruptedException
runnable in a thread named threadName.
This may be a newly created thread or it may be a thread that was
was already used to run one or more previous invocations.
runnable can spawn other threads in the pooled
ThreadGroup, but they must all exit before the runnable
completes. Failure of the extra threads to complete will result
in a severe log message and the dropping of this thread from the
pool.
This method will block until the thread begins executing
runnable. If executing Runnable.run() on
runnable throws an exception, the thread will not be
returned to the thread pool.
InterruptedExceptionpublic int waitingThreadCount()
public static CountDownLatch resetCurrentThread() throws InterruptedException
start(java.lang.String, java.lang.Runnable), this method will arrange for it to expect
to be "restarted." See RestartableThread for more
information.IllegalStateException - If the current thread is not a main
thread.InterruptedExceptionCopyright © 2022. All rights reserved.