Class KQueue
java.lang.Object
ch.k43.util.KQueue
- All Implemented Interfaces:
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clear (empty) the queue.void
close()
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
getMode()
Get the mode of the queue (FIFO or LIFO).getName()
Get the name of the queue.int
getSize()
Get number of elements in queue.boolean
isClosed()
Check if queue is closed.boolean
isEmpty()
Check if queue is empty.static KQueue
Return the queue if present.peek()
Return the next queue element without removing it.boolean
Add an element to the queue.void
setMode
(int argQueueMode) Set queue mode (LIFO or FIFO).toString()
String representation of object.
-
Field Details
-
FIFO
public static final int FIFOFirst-in-first out queue mode- See Also:
-
LIFO
public static final int LIFOLast-in-first out queue mode- See Also:
-
-
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
Create a FIFO or LIFO named queue.- Parameters:
argQueueName
- The name of the queueargQueueMode
- The type of the queue (LIFO or FIFO)
-
-
Method Details
-
getAllNames
-
open
-
clear
public void clear()Clear (empty) the queue. -
close
-
get
Return the next queue element and wait if queue is empty.- Returns:
- Queue element or null for errors
-
get
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
-
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
Return the next queue element without removing it.- Returns:
- Queue element or null if no element found or error
-
put
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
-