- Type Parameters:
K- Key -- type of client
W- Work -- type of work item
This is a generic implementation of the channels specification
in Channeling Work, Nov 2010 (channels.pdf).
Objects of type K must be registered, with
and then they become clients and a queue of
items (type W) is stored for each client.
addWorkItem(Object, Object). If the client is dormant it becomes ready thereby. All other states remain unchanged. The next ready client, together with a collection of its items, may be retrieved with
nextWorkBlock(collection,max)(making that client in progress). An in progress client can finish (processing a batch of items) with
finishWorkBlock(K). It then becomes either dormant or ready, depending if its queue of work items is empty or no. If a client has items queued, it is either in progress or ready but cannot be both. When work is finished it may be marked ready if there is further work, or dormant if there is not. There is never any work for a dormant client. A client may be unregistered, with
unregisterKey(K), which removes the client from all parts of the state, and any queue of items stored with it. All clients may be unregistered with
Concurrent SemanticsThis implementation is thread-safe.
Method SummaryModifier and TypeMethodDescription
booleanAdd (enqueue) an item for a specific client.
booleanSet client no longer in progress.
voidReturn the next ready client, and transfer a collection of that client's items to process.
keyto pool of item queues, with an empty queue.
voidRemove all clients from pool and from any other state.
voidRemove client from pool and from any other state.
registerKeypublic void registerKey
(K key)Add client
keyto pool of item queues, with an empty queue. A client is initially dormant. No-op if
key- client to add to pool
limitpublic void limit
unlimitpublic void unlimit
unregisterKeypublic void unregisterKey
(K key)Remove client from pool and from any other state. Has no effect if client already absent.
key- of client to unregister
unregisterAllKeyspublic void unregisterAllKeys()Remove all clients from pool and from any other state.
nextWorkBlockReturn the next ready client, and transfer a collection of that client's items to process. Mark client in progress. If there is no ready client, return
to- collection object in which to transfer items
size- max number of items to transfer
- key of client to whom items belong, or
nullif there is none.
addWorkItemAdd (enqueue) an item for a specific client. No change and returns
falseif client not registered. If dormant, the client will be marked ready.
key- the client to add to the work item to
item- the work item to add to the client queue
trueif and only if the client is marked ready — as a result of this work item
finishWorkBlockpublic boolean finishWorkBlock
(K key)Set client no longer in progress. Ignore unknown clients (and return
key- client that has finished work
trueif and only if client becomes ready
IllegalStateException- if registered client not in progress