Class ThreadReceivePort<Message>
- java.lang.Object
-
- co.paralleluniverse.strands.channels.ThreadReceivePort<Message>
-
public class ThreadReceivePort<Message> extends java.lang.Object
This class is a simple convenience wrapper aroundReceivePort
that 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 aReceivePort
in a thread.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the channel so that no more messages could be sent to it.boolean
equals(java.lang.Object obj)
int
hashCode()
boolean
isClosed()
Tests whether the channel has been closed and no more messages await in the channel.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.java.lang.String
toString()
Message
tryReceive()
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 aReceivePort
in a thread.- Parameters:
p
- theSendPort
to 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
null
if 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
null
if 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
null
if 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 returnstrue
all future calls toreceive()
are guaranteed to returnnull
, and calls toreceive
on a primitive channel will throw anEOFException
.- Returns:
true
if the channels has been closed and no more messages will be received;false
otherwise.
-
hashCode
public final int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public final boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
toString
public final java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-