Package com.yahoo.concurrent
Class Receiver<T>
java.lang.Object
com.yahoo.concurrent.Receiver<T>
A class for sending single messages between threads with timeout. Typical use
would be
Receiver<SomeMessage> receiver = new Receiver<SomeMessage>(); SomeRunnable runnable = new SomeRunnable(receiver); Thread worker = new Thread(runnable); worker.start(); Pair<Receiver.MessageState, SomeMessage> answer = receiver.get(500L);... and in the worker thread simply
receiver.put(new SomeMessage(...))
Any number of threads may wait for the same message. Sending null references is supported. The object is intended for delivering only single message, there is no support for recycling it.
- Author:
- Steinar Knutsen
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
MessageState is the reason for returning from get(). -
Constructor Summary
-
Method Summary
-
Constructor Details
-
Receiver
public Receiver()
-
-
Method Details
-
put
Make a message available for consumers.- Parameters:
message
- the message to send- Throws:
IllegalStateException
- if a message has already been received here
-
get
Wait for up to "timeout" milliseconds for an incoming message. This hides spurious wakeup, but InterruptedException will be propagated.- Parameters:
timeout
- maximum time to wait for message in milliseconds- Returns:
- a Pair instance containing the reason for returning and the message possible received
- Throws:
InterruptedException
- if the waiting thread is interrupted
-