TheAlgorithms/Java · error · IllegalArgumentException

Capacity must be greater than zero.

Error message

Capacity must be greater than zero.

What it means

Thrown by the ThreadSafeQueue(int capacity) constructor as an IllegalArgumentException when capacity <= 0. The queue allocates an Object[capacity] ring buffer immediately and relies on capacity for modular arithmetic (tail = (tail+1) % capacity), so a non-positive value is structurally invalid and rejected up front.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/queues/ThreadSafeQueue.java:32

public class ThreadSafeQueue<T> {

    private final Object[] buffer;
    private final int capacity;
    private int head;
    private int tail;
    private int count;
    private final ReentrantLock lock;
    private final Condition notFull;
    private final Condition notEmpty;

    /**
     * @brief Constructs a ThreadSafeQueue with the specified capacity
     * @param capacity the maximum number of elements the queue can hold
     * @throws IllegalArgumentException if capacity is less than or equal to zero
     */
    public ThreadSafeQueue(int capacity) {
        if (capacity <= 0) {
            throw new IllegalArgumentException("Capacity must be greater than zero.");
        }
        this.capacity = capacity;
        this.buffer = new Object[capacity];
        this.head = 0;
        this.tail = 0;
        this.count = 0;
        this.lock = new ReentrantLock();
        this.notFull = lock.newCondition();
        this.notEmpty = lock.newCondition();
    }

    /**
     * @brief Adds an element to the tail of the queue, blocking if full
     * @param item the element to add
     * @throws InterruptedException if the thread is interrupted while waiting
     * @throws IllegalArgumentException if the item is null
     */
    public void enqueue(T item) throws InterruptedException {

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate capacity > 0 before construction and substitute a sane positive default otherwise.
  2. Clamp computed capacities with Math.max(1, value).
  3. Treat a non-positive capacity from configuration as a startup-fatal error with an explicit message.

Example fix

// before
ThreadSafeQueue<T> q = new ThreadSafeQueue<>(configuredCapacity); // may be <= 0

// after
int cap = Math.max(1, configuredCapacity);
ThreadSafeQueue<T> q = new ThreadSafeQueue<>(cap);
Defensive patterns

Strategy: validation

Validate before calling

int cap = Math.max(1, configuredCapacity);
ThreadSafeQueue<T> q = new ThreadSafeQueue<>(cap);

Type guard

boolean validCapacity = configuredCapacity > 0;

Try / catch

null

Prevention

When it happens

Trigger: Passing 0 or a negative integer to new ThreadSafeQueue<>(capacity); passing a runtime-computed capacity (config, pool size, formula) that collapses to zero or below for some inputs.

Common situations: Config-driven capacity left unset (resolves to 0); capacity computed as a difference that goes negative under small inputs; defaulted/deserialized settings producing 0; misuse where capacity is meant to mirror a thread or buffer count that is itself zero.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/52bf90e88e52baae. Report an issue: GitHub.