Class Channel<T>
java.lang.Object
org.daiitech.naftah.builtin.utils.concurrent.Channel<T>
- Type Parameters:
T- the type of values sent through the channel
A simple thread-safe channel for communicating between threads.
The Channel class wraps a BlockingQueue to allow
sending and receiving messages between threads safely.
Key characteristics:
- Supports blocking send and receive operations.
- Each channel has a name for easier identification.
- Multiple threads can safely send to and receive from the channel concurrently.
- Author:
- Chakib Daii
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetName()Returns the name of this channel.static <T> Channel<T>Factory method to create a new channel with the given name.receive()Receives a value from the channel, blocking if the queue is empty.voidSends a value into the channel, blocking if the queue is full.toString()Returns a string representation of the channel.
-
Field Details
-
queue
-
name
-
-
Constructor Details
-
Channel
Creates a channel with the specified name.- Parameters:
name- the name of the channel
-
-
Method Details
-
of
Factory method to create a new channel with the given name.- Type Parameters:
T- the type of values the channel carries- Parameters:
name- the name of the channel- Returns:
- a new
Channelinstance
-
send
Sends a value into the channel, blocking if the queue is full.- Parameters:
value- the value to send- Throws:
InterruptedException- if the thread is interrupted while waiting
-
receive
Receives a value from the channel, blocking if the queue is empty.- Returns:
- the next value from the channel
- Throws:
InterruptedException- if the thread is interrupted while waiting
-
getName
Returns the name of this channel.The name is assigned when the channel is created using
of(String). It is used primarily for identification and debugging, especially when multiple channels are involved in concurrent operations.- Returns:
- the name of the channel
-
toString
Returns a string representation of the channel.
-