java.lang.Object
org.daiitech.naftah.builtin.utils.concurrent.Channel<T>
Type Parameters:
T - the type of values sent through the channel

public final class Channel<T> extends Object
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
    Modifier and Type
    Field
    Description
    private final String
     
    private final BlockingQueue<T>
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Creates a channel with the specified name.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the name of this channel.
    static <T> Channel<T>
    of(String name)
    Factory method to create a new channel with the given name.
    Receives a value from the channel, blocking if the queue is empty.
    void
    send(T value)
    Sends a value into the channel, blocking if the queue is full.
    Returns a string representation of the channel.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • Channel

      private Channel(String name)
      Creates a channel with the specified name.
      Parameters:
      name - the name of the channel
  • Method Details

    • of

      public static <T> Channel<T> of(String name)
      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 Channel instance
    • send

      public void send(T value) throws InterruptedException
      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

      public T receive() throws InterruptedException
      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

      public String 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

      public String toString()
      Returns a string representation of the channel.
      Overrides:
      toString in class Object
      Returns:
      a string in the format "<قناة name>"