Class KQueue

java.lang.Object
ch.k43.util.KQueue
All Implemented Interfaces:
AutoCloseable

public final class KQueue extends Object implements AutoCloseable
Implements a fast, simple, thread-safe and named queue as FIFO (first-in-first-out) or LIFO (last-in-first-out).

The name of the queue may be used by other threads to open and access the queue.

Use try-with-resources or explicit close() to ensure queue resources are cleaned up:

   try (KQueue q = new KQueue("MyQueue", KQueue.FIFO)) {
   ...
   }
 
Since:
2025.09.15
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    First-in-first out queue mode
    static final int
    Last-in-first out queue mode
  • Constructor Summary

    Constructors
    Constructor
    Description
    KQueue(int argQueueMode)
    Create a FIFO or LIFO queue.
    KQueue(String argQueueName, int argQueueMode)
    Create a FIFO or LIFO named queue.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clear (empty) the queue.
    void
    Close the queue.
    get()
    Return the next queue element and wait if queue is empty.
    get(boolean argWait)
    Return the next queue element and optionally wait if queue is empty.
    static String[]
    Return a list of all queue names.
    int
    Get the mode of the queue (FIFO or LIFO).
    Get the name of the queue.
    int
    Get number of elements in queue.
    boolean
    Check if queue is closed.
    boolean
    Check if queue is empty.
    static KQueue
    open(String argQueueName)
    Return the queue if present.
    Return the next queue element without removing it.
    boolean
    put(Object argObject)
    Add an element to the queue.
    void
    setMode(int argQueueMode)
    Set queue mode (LIFO or FIFO).
    String representation of object.

    Methods inherited from class java.lang.Object

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

  • Constructor Details

    • KQueue

      public KQueue(int argQueueMode)
      Create a FIFO or LIFO queue. The generated name may be obtained thru getName().
      Parameters:
      argQueueMode - The type of the queue (LIFO or FIFO)
    • KQueue

      public KQueue(String argQueueName, int argQueueMode)
      Create a FIFO or LIFO named queue.
      Parameters:
      argQueueName - The name of the queue
      argQueueMode - The type of the queue (LIFO or FIFO)
  • Method Details

    • getAllNames

      public static String[] getAllNames()
      Return a list of all queue names.
      Returns:
      List of names
    • open

      public static KQueue open(String argQueueName)
      Return the queue if present.
      Parameters:
      argQueueName - The name of the queue
      Returns:
      Queue object
    • clear

      public void clear()
      Clear (empty) the queue.
    • close

      public void close()
      Close the queue.
      Specified by:
      close in interface AutoCloseable
    • get

      public Object get()
      Return the next queue element and wait if queue is empty.
      Returns:
      Queue element or null for errors
    • get

      public Object get(boolean argWait)
      Return the next queue element and optionally wait if queue is empty.
      Parameters:
      argWait - If true, wait for next element, false otherwise
      Returns:
      Queue element or null if no element found or error
    • getMode

      public int getMode()
      Get the mode of the queue (FIFO or LIFO).
      Returns:
      FIFO or LIFO mode
    • getName

      public String getName()
      Get the name of the queue.
      Returns:
      Name of the queue
    • getSize

      public int getSize()
      Get number of elements in queue.
      Returns:
      Number of elements
    • isClosed

      public boolean isClosed()
      Check if queue is closed.
      Returns:
      True if queue is closed, false otherwise
    • isEmpty

      public boolean isEmpty()
      Check if queue is empty.
      Returns:
      True if queue is empty, false otherwise
    • peek

      public Object peek()
      Return the next queue element without removing it.
      Returns:
      Queue element or null if no element found or error
    • put

      public boolean put(Object argObject)
      Add an element to the queue.
      Parameters:
      argObject - Element to add to the queue
      Returns:
      True if success, false otherwise
    • setMode

      public void setMode(int argQueueMode)
      Set queue mode (LIFO or FIFO).
      Parameters:
      argQueueMode - LIFO or FIFO queue mode
    • toString

      public String toString()
      String representation of object.
      Overrides:
      toString in class Object
      Returns:
      Object representation