public class Dispatch extends Object
The Dispatch class is used to get or create dispatch objects such as global queues, thread queues, serial queues, or dispatch sources.
It is encouraged that end users of this api do a static import of the methods defined in this class.
import static org.fusesource.hawtdispatch.Dispatch.*;
The dispatch queues are Executor
objects that execute tasks asynchronously on thread pools managed by the
Dispatcher.
All dispatch queues use a shared fixed size thread pool to execute tasks. All tasks submitted on a dispatch queue should be non-blocking and wait free.
Dispatch sources provide a way to trigger execution of a user task on on a user selected dispatch queue in response to NIO or application defined events.
Modifier and Type | Field and Description |
---|---|
static DispatchPriority |
DEFAULT |
static DispatchPriority |
HIGH |
static DispatchPriority |
LOW |
static Task |
NOOP
A Runnable task that does nothing.
|
Constructor and Description |
---|
Dispatch() |
Modifier and Type | Method and Description |
---|---|
static DispatchQueue |
createQueue()
Creates a new serial dispatch queue to which runnable objects may be submitted.
|
static DispatchQueue |
createQueue(String label)
Creates a new serial dispatch queue to which runnable objects may be submitted.
|
static <Event,MergedEvent> |
createSource(EventAggregator<Event,MergedEvent> aggregator,
DispatchQueue queue)
Creates a new
CustomDispatchSource to monitor events merged into
the dispatch source and automatically submit a handler runnable to a dispatch queue
in response to the events. |
static DispatchSource |
createSource(SelectableChannel channel,
int interestOps,
DispatchQueue queue)
Creates a new
DispatchSource to monitor SelectableChannel objects and
automatically submit a handler runnable to a dispatch queue in response to events. |
static DispatchQueue |
getCurrentQueue()
Returns the queue on which the currently executing runnable is running.
|
static DispatchQueue |
getCurrentThreadQueue() |
static DispatchQueue |
getGlobalQueue()
Returns the global concurrent queue of default priority.
|
static DispatchQueue |
getGlobalQueue(DispatchPriority priority)
Returns a well-known global concurrent queue of a given priority level.
|
static DispatchQueue[] |
getThreadQueues(DispatchPriority priority) |
static List<Metrics> |
metrics()
Used to get profiling metrics for all the queues
currently being profiled.
|
static void |
profile(boolean enabled)
If enabled then it enables profiling on the global
queues and any newly created queues.
|
static void |
restart()
Restart default dispatcher instance.
|
static void |
shutdown()
Shutdown default dispatcher instance.
|
public static final DispatchPriority HIGH
public static final DispatchPriority DEFAULT
public static final DispatchPriority LOW
public Dispatch()
public static DispatchQueue getGlobalQueue()
Returns the global concurrent queue of default priority.
getGlobalQueue(DispatchPriority)
public static DispatchQueue getGlobalQueue(DispatchPriority priority)
Returns a well-known global concurrent queue of a given priority level.
The well-known global concurrent queues may not be modified. Calls to
Suspendable.suspend()
, Suspendable.resume()
, etc., will
have no effect when used with queues returned by this function.
priority
- A priority defined in dispatch_queue_priority_tpublic static DispatchQueue createQueue(String label)
Creates a new serial dispatch queue to which runnable objects may be submitted.
Serial dispatch queues execute runnables submitted to them serially in FIFO order. A queue will only invoke one runnable at a time, but independent queues may each execute their runnables concurrently with respect to each other.
Conceptually a dispatch queue may have its own thread of execution, and interaction between queues is highly asynchronous.
label
- the label to assign the dispatch queue, can be nullpublic static DispatchQueue createQueue()
Creates a new serial dispatch queue to which runnable objects may be submitted.
Same thing as createQueue(null)
createQueue(String)
public static DispatchQueue getCurrentQueue()
Returns the queue on which the currently executing runnable is running.
When getCurrentQueue()
is called outside of the context of a
submitted runnable, it will return null.
public static DispatchSource createSource(SelectableChannel channel, int interestOps, DispatchQueue queue)
Creates a new DispatchSource
to monitor SelectableChannel
objects and
automatically submit a handler runnable to a dispatch queue in response to events.
You are allowed to create multiple dispatch sources to the same SelectableChannel
object.
channel
- the channel to monitor.interestOps
- A mask of interest ops (SelectionKey.OP_ACCEPT
,
SelectionKey.OP_CONNECT
, SelectionKey.OP_READ
, or
SelectionKey.OP_WRITE
) specifying which events are desired.queue
- The dispatch queue to which the event handler tasks will be submited.public static <Event,MergedEvent> CustomDispatchSource<Event,MergedEvent> createSource(EventAggregator<Event,MergedEvent> aggregator, DispatchQueue queue)
Creates a new CustomDispatchSource
to monitor events merged into
the dispatch source and automatically submit a handler runnable to a dispatch queue
in response to the events.
aggregator
- the data aggregation strategy to use.queue
- The dispatch queue to which the event handler tasks will be submited.public static DispatchQueue[] getThreadQueues(DispatchPriority priority)
public static DispatchQueue getCurrentThreadQueue()
public static void profile(boolean enabled)
enabled
- public static List<Metrics> metrics()
public static void shutdown()
public static void restart()
Copyright © 2010–2017 FuseSource, Corp.. All rights reserved.