Class Channel<T>

java.lang.Object
com.softwaremill.jox.Channel<T>
Type Parameters:
T - The type of the values processed by the channel.
All Implemented Interfaces:
CloseableChannel, Sink<T>, Source<T>

public final class Channel<T> extends Object implements Source<T>, Sink<T>
Channel is a thread-safe data structure which exposes three basic operations:

- send(Object)-ing a value to the channel. Values can't be null. - receive()-ing a value from the channel - closing the channel using done() or error(Throwable)

There are three channel flavors:

- rendezvous channels, where senders and receivers must meet to exchange values - buffered channels, where a given number of sent values might be buffered, before subsequent `send`s block - unlimited channels, where an unlimited number of values might be buffered, hence `send` never blocks

To create a channel, use newRendezvousChannel(), newBufferedChannel(int) and newUnlimitedChannel() methods. Additionally, newBufferedDefaultChannel() creates a buffered channel with a "default" capacity of 16, which should be a good starting point for most use-cases.

In a rendezvous channel, senders and receivers block, until a matching party arrives (unless one is already waiting). Similarly, buffered channels block if the buffer is full (in case of senders), or in case of receivers, if the buffer is empty and there are no waiting senders.

All blocking operations behave properly upon interruption.

Channels might be closed, either because no more values will be produced by the source (using done()), or because there was an error while producing or processing the received values (using error(Throwable)).

After closing, no more values can be sent to the channel. If the channel is "done", any pending sends will be completed normally. If the channel is in an "error" state, pending sends will be interrupted and will return with the reason for the closure.

In case the channel is closed, one of the ChannelClosedExceptions is thrown. Alternatively, you can call the less type-safe, but more exception-safe sendOrClosed(Object) and receiveOrClosed() methods, which do not throw in case the channel is closed, but return one of the ChannelClosed values.