public class ExecutorComponent
extends java.lang.Object
implements java.io.Closeable
Every method in this class returns a com.google.common.util.concurrent.ListeningExecutorService
, so that you
can listen for task completion if you would like.
If you're looking for scheduled thread pools, use ScheduledRunnableComponent instead. It handles more of that for you.
Modifier and Type | Class and Description |
---|---|
static class |
ExecutorComponent.AbortPolicy
Just like the java.util.concurrent.ThreadPoolExecutor.AbortPolicy, but with a better error message.
|
class |
ExecutorComponent.ListeningExecutorServiceBuilder |
Constructor and Description |
---|
ExecutorComponent(MetricRegistry metricRegistry) |
Modifier and Type | Method and Description |
---|---|
void |
close() |
ListeningExecutorService |
getOrCreateCachedThreadPool(java.lang.String threadNameFormat) |
ListeningExecutorService |
getOrCreateSingleThreadExecutor(java.lang.String threadPoolName,
java.lang.String threadNameFormat)
TODO: experimental.
|
ExecutorComponent.ListeningExecutorServiceBuilder |
newBuilder() |
ListeningExecutorService |
newCachedThreadPool(java.lang.String threadNameFormat)
Creates a thread pool that creates new threads as needed, but
will reuse previously constructed threads when they are
available.
|
ListeningExecutorService |
newFixedThreadPool(int nThreads,
java.lang.String threadNameFormat)
Creates a thread pool that reuses a fixed number of threads
operating off a shared unbounded queue, using the provided
ThreadFactory to create new threads when needed.
|
ListeningExecutorService |
newSingleThreadExecutor(java.lang.String threadNameFormat) |
public ListeningExecutorService getOrCreateSingleThreadExecutor(java.lang.String threadPoolName, java.lang.String threadNameFormat)
threadPoolName
- the name of the thread pool to get. Multiple requests with the same thread pool name will
return the same thread pool.threadNameFormat
- a String.format(String, Object...)
-compatible format String, to which a unique
integer (0, 1, etc.) will be supplied as the single parameter. This integer will be unique
to the built instance of the ThreadFactory and will be assigned sequentially. For example,
"rpc-pool-%d"
will generate thread names like "rpc-pool-0"
,
"rpc-pool-1"
, "rpc-pool-2"
, etc.public ListeningExecutorService newSingleThreadExecutor(java.lang.String threadNameFormat)
threadNameFormat
- a String.format(String, Object...)
-compatible
format String, to which a unique integer (0, 1, etc.) will be supplied
as the single parameter. This integer will be unique to the built
instance of the ThreadFactory and will be assigned sequentially. For
example, "rpc-pool-%d"
will generate thread names like
"rpc-pool-0"
, "rpc-pool-1"
, "rpc-pool-2"
, etc.public ListeningExecutorService newFixedThreadPool(int nThreads, java.lang.String threadNameFormat)
shutdown
.nThreads
- the number of threads in the poolthreadNameFormat
- a String.format(String, Object...)
-compatible
format String, to which a unique integer (0, 1, etc.) will be supplied
as the single parameter. This integer will be unique to the built
instance of the ThreadFactory and will be assigned sequentially. For
example, "rpc-pool-%d"
will generate thread names like
"rpc-pool-0"
, "rpc-pool-1"
, "rpc-pool-2"
, etc.public ListeningExecutorService newCachedThreadPool(java.lang.String threadNameFormat)
ThreadPoolExecutor
constructors.
Note that unlike java.util.concurrent.Executors#newCachedThreadPool, this implementation does limit the pool size
to 15,000 threads, since that is a practical limit on a Linux machine with ~16gb of RAM. That is based on the
assumptions that: 1.) if you hit that limit, something is going wrong in your system, and 2.) it's at least
marginally better to fail at the point of execution with a RejectedExecutionException versus an OutOfMemoryError
with a "could not create new native thread" message. Both are probably terminal, but at least the former may be
slightly more understandable?threadNameFormat
- a String.format(String, Object...)
-compatible
format String, to which a unique integer (0, 1, etc.) will be supplied
as the single parameter. This integer will be unique to the built
instance of the ThreadFactory and will be assigned sequentially. For
example, "rpc-pool-%d"
will generate thread names like
"rpc-pool-0"
, "rpc-pool-1"
, "rpc-pool-2"
, etc.public ListeningExecutorService getOrCreateCachedThreadPool(java.lang.String threadNameFormat)
public ExecutorComponent.ListeningExecutorServiceBuilder newBuilder()
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException