Class TransferChannel<Message>
- java.lang.Object
-
- co.paralleluniverse.strands.channels.TransferChannel<Message>
-
- All Implemented Interfaces:
Channel<Message>
,Port<Message>
,PortAutoCloseable
,ReceivePort<Message>
,Selectable<Message>
,SendPort<Message>
,StandardChannel<Message>
,Synchronization
,java.lang.AutoCloseable
- Direct Known Subclasses:
TimeoutChannel
public class TransferChannel<Message> extends java.lang.Object implements StandardChannel<Message>, Selectable<Message>, Synchronization
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface co.paralleluniverse.strands.channels.ReceivePort
ReceivePort.EOFException
-
-
Constructor Summary
Constructors Constructor Description TransferChannel()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
capacity()
The channel's internal buffer capacity.void
close()
Closes the channel so that no more messages could be sent to it.void
close(java.lang.Throwable t)
Closes the channel so that no more messages could be sent to it, and signifies an exception occurred in the producer.boolean
equals(java.lang.Object other)
int
getWaitingConsumerCount()
boolean
isClosed()
Tests whether the channel has been closed and no more messages await in the channel.boolean
isSingleConsumer()
Whether or not the channel supports a single consumer only.boolean
isSingleProducer()
Whether or not the channel supports a single producer only.Message
receive()
Retrieves a message from the channels, possibly blocking until one becomes available.Message
receive(long timeout, java.util.concurrent.TimeUnit unit)
Retrieves a message from the channels, possibly blocking until one becomes available, but no longer than the specified timeout.Message
receive(Timeout timeout)
Retrieves a message from the channels, possibly blocking until one becomes available, but no longer than the specified timeout.protected Message
receiveInternal(long timeout, java.util.concurrent.TimeUnit unit)
java.lang.Object
register()
java.lang.Object
register(SelectAction<Message> action)
void
send(Message message)
Sends a message to the channel, possibly blocking until there's room available in the channel.boolean
send(Message message, long timeout, java.util.concurrent.TimeUnit unit)
Sends a message to the channel, possibly blocking until there's room available in the channel, but never longer than the specified timeout.boolean
send(Message message, Timeout timeout)
Sends a message to the channel, possibly blocking until there's room available in the channel, but never longer than the specified timeout.boolean
tryNow(java.lang.Object token)
Message
tryReceive()
Retrieves a message from the channel if one is available.boolean
trySend(Message message)
Sends a message to the channel if the channel has room available.void
unregister(java.lang.Object token)
-
-
-
Method Detail
-
capacity
public final int capacity()
Description copied from interface:StandardChannel
The channel's internal buffer capacity.- Specified by:
capacity
in interfaceStandardChannel<Message>
- Returns:
- the channel's internal buffer capacity,
-1
for an unbounded buffer, and0
for a transfer channel.
-
isSingleProducer
public boolean isSingleProducer()
Description copied from interface:StandardChannel
Whether or not the channel supports a single producer only.- Specified by:
isSingleProducer
in interfaceStandardChannel<Message>
- Returns:
true
if the channel supports no more than one producer;false
otherwise.
-
isSingleConsumer
public boolean isSingleConsumer()
Description copied from interface:StandardChannel
Whether or not the channel supports a single consumer only.- Specified by:
isSingleConsumer
in interfaceStandardChannel<Message>
- Returns:
true
if the channel supports no more than one consumer;false
otherwise.
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
send
public void send(Message message) throws SuspendExecution, java.lang.InterruptedException
Description copied from interface:SendPort
Sends a message to the channel, possibly blocking until there's room available in the channel. If the channel is full, this method may block, throw an exception, silently drop the message, or displace an old message from the channel. The behavior is determined by the channel'sOverflowPolicy
, set at construction time.- Specified by:
send
in interfaceSendPort<Message>
- Parameters:
message
- the message- Throws:
SuspendExecution
java.lang.InterruptedException
-
send
public boolean send(Message message, long timeout, java.util.concurrent.TimeUnit unit) throws SuspendExecution, java.lang.InterruptedException
Description copied from interface:SendPort
Sends a message to the channel, possibly blocking until there's room available in the channel, but never longer than the specified timeout. If the channel is full, this method may block, throw an exception, silently drop the message, or displace an old message from the channel. The behavior is determined by the channel'sOverflowPolicy
, set at construction time.- Specified by:
send
in interfaceSendPort<Message>
- Parameters:
message
- the messagetimeout
- the maximum duration this method is allowed to wait.unit
- the timeout's time unit- Returns:
true
if the message has been sent successfully;false
if the timeout has expired.- Throws:
SuspendExecution
java.lang.InterruptedException
-
send
public boolean send(Message message, Timeout timeout) throws SuspendExecution, java.lang.InterruptedException
Description copied from interface:SendPort
Sends a message to the channel, possibly blocking until there's room available in the channel, but never longer than the specified timeout. If the channel is full, this method may block, throw an exception, silently drop the message, or displace an old message from the channel. The behavior is determined by the channel'sOverflowPolicy
, set at construction time.- Specified by:
send
in interfaceSendPort<Message>
- Parameters:
message
- the messagetimeout
- the method will not block for longer than the amount remaining in theTimeout
- Returns:
true
if the message has been sent successfully;false
if the timeout has expired.- Throws:
SuspendExecution
java.lang.InterruptedException
-
trySend
public boolean trySend(Message message)
Description copied from interface:SendPort
Sends a message to the channel if the channel has room available. This method never blocks.
-
close
public void close()
Description copied from interface:PortAutoCloseable
Closes the channel so that no more messages could be sent to it. Messages already sent to the channel will still be received.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacePortAutoCloseable
-
close
public void close(java.lang.Throwable t)
Description copied from interface:SendPort
Closes the channel so that no more messages could be sent to it, and signifies an exception occurred in the producer. The exception will be thrown when the consumer callsReceivePort
'sreceive
ortryReceive
, wrapped by aProducerException
. Messages already sent to the channel prior to calling this method will still be received.
-
tryReceive
public Message tryReceive()
Description copied from interface:ReceivePort
Retrieves a message from the channel if one is available. This method never blocks.- Specified by:
tryReceive
in interfaceReceivePort<Message>
- Returns:
- a message, or
null
if one is not immediately available.
-
register
public java.lang.Object register()
- Specified by:
register
in interfaceSynchronization
-
register
public java.lang.Object register(SelectAction<Message> action)
- Specified by:
register
in interfaceSelectable<Message>
-
tryNow
public boolean tryNow(java.lang.Object token)
- Specified by:
tryNow
in interfaceSelectable<Message>
-
unregister
public void unregister(java.lang.Object token)
- Specified by:
unregister
in interfaceSelectable<Message>
- Specified by:
unregister
in interfaceSynchronization
-
receive
public Message receive() throws SuspendExecution, java.lang.InterruptedException
Description copied from interface:ReceivePort
Retrieves a message from the channels, possibly blocking until one becomes available.- Specified by:
receive
in interfaceReceivePort<Message>
- Returns:
- a message, or
null
if the channel has been closed and no more messages await (seePortAutoCloseable.isClosed()
). - Throws:
SuspendExecution
java.lang.InterruptedException
-
receiveInternal
protected Message receiveInternal(long timeout, java.util.concurrent.TimeUnit unit) throws SuspendExecution, java.lang.InterruptedException
- Throws:
SuspendExecution
java.lang.InterruptedException
-
receive
public Message receive(long timeout, java.util.concurrent.TimeUnit unit) throws SuspendExecution, java.lang.InterruptedException
Description copied from interface:ReceivePort
Retrieves a message from the channels, possibly blocking until one becomes available, but no longer than the specified timeout.- Specified by:
receive
in interfaceReceivePort<Message>
- Parameters:
timeout
- the maximum duration to block waiting for a message.unit
- the time unit of the timeout.- Returns:
- a message, or
null
if the channel has been closed and no more messages await (seePortAutoCloseable.isClosed()
), or if the timeout has expired. - Throws:
SuspendExecution
java.lang.InterruptedException
-
receive
public Message receive(Timeout timeout) throws SuspendExecution, java.lang.InterruptedException
Description copied from interface:ReceivePort
Retrieves a message from the channels, possibly blocking until one becomes available, but no longer than the specified timeout.- Specified by:
receive
in interfaceReceivePort<Message>
- Parameters:
timeout
- the method will not block for longer than the amount remaining in theTimeout
- Returns:
- a message, or
null
if the channel has been closed and no more messages await (seePortAutoCloseable.isClosed()
), or if the timeout has expired. - Throws:
SuspendExecution
java.lang.InterruptedException
-
isClosed
public boolean isClosed()
Description copied from interface:PortAutoCloseable
Tests whether the channel has been closed and no more messages await in the channel. If this method returnstrue
all future calls toreceive
are guaranteed to returnnull
, and calls toreceive
on a primitive channel will throw aEOFException
.- Specified by:
isClosed
in interfacePortAutoCloseable
- Returns:
true
if the channels has been closed and no more messages will be received;false
otherwise.
-
getWaitingConsumerCount
public int getWaitingConsumerCount()
-
-