public class UnfairExecutor extends AbstractSubmitterExecutor
SubmitterExecutor
implementation. Though to get those
performance gains, some guarantees are reduced. Most prominently is execution order, this
scheduler does not ensure that tasks are executed in the order they are submitted, but rather
tasks are consumed however is fastest.
This executor has an execution queue per thread. That way each thread has a many producer, one
consumer safety guarantees. Compared to other pools which have to deal with a many to many
thread safety issue. Determining the thread queue may be based on a variety of information. A
couple built in distribution solutions are UnfairExecutor.TaskHashXorTimeStripeGenerator
(default) and
UnfairExecutor.AtomicStripeGenerator
.
This scheduler will work best when the following conditions are true. First because a long running task can block other tasks from running (even when other threads are idle). It is best that tasks should be equally sized. We also recommend having thread counts which are prime numbers for a more even hash distribution. It is also important to recognize that it is generally a bad idea to block any of these threads waiting for results from processing that is expected to happen on the same executor.
Modifier and Type | Class and Description |
---|---|
static class |
UnfairExecutor.AtomicStripeGenerator
Stripe generator which will round robin distribute tasks to threads.
|
static class |
UnfairExecutor.TaskHashXorTimeStripeGenerator
Generator which will determine the task stripe by using the identity hash of the runnable and
Clock.lastKnownTimeNanos() . |
static interface |
UnfairExecutor.TaskStripeGenerator
Strategy for taking in a task and producing a long which will be translated to which thread
the task should be distributed on to.
|
Constructor and Description |
---|
UnfairExecutor(int threadCount)
Constructs a new
UnfairExecutor with a provided thread count. |
UnfairExecutor(int threadCount,
boolean useDaemonThreads)
Constructs a new
UnfairExecutor with a provided thread count. |
UnfairExecutor(int threadCount,
boolean useDaemonThreads,
UnfairExecutor.TaskStripeGenerator stripeGenerator)
Constructs a new
UnfairExecutor with a provided thread count. |
UnfairExecutor(int threadCount,
java.util.concurrent.ThreadFactory threadFactory)
Constructs a new
UnfairExecutor with a provided thread count and factory. |
UnfairExecutor(int threadCount,
java.util.concurrent.ThreadFactory threadFactory,
UnfairExecutor.TaskStripeGenerator stripeGenerator)
Constructs a new
UnfairExecutor with a provided thread count and factory. |
UnfairExecutor(int threadCount,
UnfairExecutor.TaskStripeGenerator stripeGenerator)
Constructs a new
UnfairExecutor with a provided thread count. |
Modifier and Type | Method and Description |
---|---|
void |
awaitTermination()
Block until the thread pool has shutdown and all threads have been stopped.
|
boolean |
awaitTermination(long timeoutMillis)
Block until the thread pool has shutdown and all threads have been stopped.
|
boolean |
isShutdown()
Function to check if the thread pool is currently accepting and handling tasks.
|
void |
shutdown()
Stops any new tasks from being submitted to the pool.
|
java.util.List<java.lang.Runnable> |
shutdownNow()
Stops any new tasks from being submitted to the pool.
|
execute, submit, submit
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
submit
public UnfairExecutor(int threadCount)
UnfairExecutor
with a provided thread count. This defaults to using
daemon threads. This also defaults to using the UnfairExecutor.TaskHashXorTimeStripeGenerator
.threadCount
- Number of threads, recommended to be a prime numberpublic UnfairExecutor(int threadCount, UnfairExecutor.TaskStripeGenerator stripeGenerator)
UnfairExecutor
with a provided thread count. This defaults to using
daemon threads.
Possible built in stripe generators for use would be UnfairExecutor.AtomicStripeGenerator
or
UnfairExecutor.TaskHashXorTimeStripeGenerator
.
threadCount
- Number of threads, recommended to be a prime numberstripeGenerator
- Generator for figuring out how a task is assigned to a threadpublic UnfairExecutor(int threadCount, boolean useDaemonThreads)
UnfairExecutor
with a provided thread count. This also defaults
to using the UnfairExecutor.TaskHashXorTimeStripeGenerator
.threadCount
- Number of threads, recommended to be a prime numberuseDaemonThreads
- true
if created threads should be daemonpublic UnfairExecutor(int threadCount, boolean useDaemonThreads, UnfairExecutor.TaskStripeGenerator stripeGenerator)
UnfairExecutor
with a provided thread count.
Possible built in stripe generators for use would be UnfairExecutor.AtomicStripeGenerator
or
UnfairExecutor.TaskHashXorTimeStripeGenerator
.
threadCount
- Number of threads, recommended to be a prime numberuseDaemonThreads
- true
if created threads should be daemonstripeGenerator
- Generator for figuring out how a task is assigned to a threadpublic UnfairExecutor(int threadCount, java.util.concurrent.ThreadFactory threadFactory)
UnfairExecutor
with a provided thread count and factory. This also
defaults to using the UnfairExecutor.TaskHashXorTimeStripeGenerator
.threadCount
- Number of threads, recommended to be a prime numberthreadFactory
- thread factory for producing new threads within executorpublic UnfairExecutor(int threadCount, java.util.concurrent.ThreadFactory threadFactory, UnfairExecutor.TaskStripeGenerator stripeGenerator)
UnfairExecutor
with a provided thread count and factory.
Possible built in stripe generators for use would be UnfairExecutor.AtomicStripeGenerator
or
UnfairExecutor.TaskHashXorTimeStripeGenerator
.
threadCount
- Number of threads, recommended to be a prime numberthreadFactory
- thread factory for producing new threads within executorstripeGenerator
- Generator for figuring out how a task is assigned to a threadpublic boolean isShutdown()
true
if thread pool is runningpublic void shutdown()
shutdown()
or
shutdownNow()
has already been called, this will have no effect.
If you wish to not want to run any queued tasks you should use shutdownNow()
.
public java.util.List<java.lang.Runnable> shutdownNow()
public void awaitTermination() throws java.lang.InterruptedException
shutdown()
or shutdownNow()
is invoked, then this will block forever.java.lang.InterruptedException
- Thrown if blocking thread is interrupted waiting for shutdownpublic boolean awaitTermination(long timeoutMillis) throws java.lang.InterruptedException
shutdown()
or shutdownNow()
is invoked, then this will block until the
timeout is reached.timeoutMillis
- time to block and wait for thread pool to shutdowntrue
if the pool has shutdown, false if timeout was reachedjava.lang.InterruptedException
- Thrown if blocking thread is interrupted waiting for shutdown