Class ThreadReceivePort<Message>
- java.lang.Object
-
- co.paralleluniverse.strands.channels.ThreadReceivePort<Message>
-
public class ThreadReceivePort<Message> extends java.lang.ObjectThis class is a simple convenience wrapper aroundReceivePortthat can be used by threads (as opposed to fibers). Its methods do not declare they throwSuspendExecution.
-
-
Constructor Summary
Constructors Constructor Description ThreadReceivePort(ReceivePort<Message> p)Creates a new convenience wrapper for using aReceivePortin a thread.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the channel so that no more messages could be sent to it.booleanequals(java.lang.Object obj)inthashCode()booleanisClosed()Tests whether the channel has been closed and no more messages await in the channel.Messagereceive()Retrieves a message from the channels, possibly blocking until one becomes available.Messagereceive(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.java.lang.StringtoString()MessagetryReceive()Retrieves a message from the channel if one is available.
-
-
-
Constructor Detail
-
ThreadReceivePort
public ThreadReceivePort(ReceivePort<Message> p)
Creates a new convenience wrapper for using aReceivePortin a thread.- Parameters:
p- theSendPortto wrap.
-
-
Method Detail
-
receive
public Message receive() throws java.lang.InterruptedException
Retrieves a message from the channels, possibly blocking until one becomes available.- Returns:
- a message, or
nullif the channel has been closed and no more messages await (seeisClosed()). - Throws:
java.lang.InterruptedException
-
receive
public Message receive(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
Retrieves a message from the channels, possibly blocking until one becomes available, but no longer than the specified timeout.- Parameters:
timeout- the maximum duration to block waiting for a message.unit- the time unit of the timeout.- Returns:
- a message, or
nullif the channel has been closed and no more messages await (seeisClosed()), or if the timeout has expired. - Throws:
java.lang.InterruptedException
-
tryReceive
public Message tryReceive()
Retrieves a message from the channel if one is available. This method never blocks.- Returns:
- a message, or
nullif one is not immediately available.
-
close
public void close()
Closes the channel so that no more messages could be sent to it. Messages already sent to the channel will still be received.
-
isClosed
public boolean isClosed()
Tests whether the channel has been closed and no more messages await in the channel. If this method returnstrueall future calls toreceive()are guaranteed to returnnull, and calls toreceiveon a primitive channel will throw anEOFException.- Returns:
trueif the channels has been closed and no more messages will be received;falseotherwise.
-
hashCode
public final int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public final boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
toString
public final java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-