{"record":{"id":"52bf90e88e52baae","repo":"TheAlgorithms/Java","slug":"capacity-must-be-greater-than-zero-52bf90","errorCode":null,"errorMessage":"Capacity must be greater than zero.","messagePattern":"Capacity must be greater than zero\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/queues/ThreadSafeQueue.java","lineNumber":32,"sourceCode":"public class ThreadSafeQueue<T> {\n\n    private final Object[] buffer;\n    private final int capacity;\n    private int head;\n    private int tail;\n    private int count;\n    private final ReentrantLock lock;\n    private final Condition notFull;\n    private final Condition notEmpty;\n\n    /**\n     * @brief Constructs a ThreadSafeQueue with the specified capacity\n     * @param capacity the maximum number of elements the queue can hold\n     * @throws IllegalArgumentException if capacity is less than or equal to zero\n     */\n    public ThreadSafeQueue(int capacity) {\n        if (capacity <= 0) {\n            throw new IllegalArgumentException(\"Capacity must be greater than zero.\");\n        }\n        this.capacity = capacity;\n        this.buffer = new Object[capacity];\n        this.head = 0;\n        this.tail = 0;\n        this.count = 0;\n        this.lock = new ReentrantLock();\n        this.notFull = lock.newCondition();\n        this.notEmpty = lock.newCondition();\n    }\n\n    /**\n     * @brief Adds an element to the tail of the queue, blocking if full\n     * @param item the element to add\n     * @throws InterruptedException if the thread is interrupted while waiting\n     * @throws IllegalArgumentException if the item is null\n     */\n    public void enqueue(T item) throws InterruptedException {","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/queues/ThreadSafeQueue.java#L14-L50","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate capacity > 0 before construction and substitute a sane positive default otherwise.","Clamp computed capacities with Math.max(1, value).","Treat a non-positive capacity from configuration as a startup-fatal error with an explicit message."],"exampleFix":"// before\nThreadSafeQueue<T> q = new ThreadSafeQueue<>(configuredCapacity); // may be <= 0\n\n// after\nint cap = Math.max(1, configuredCapacity);\nThreadSafeQueue<T> q = new ThreadSafeQueue<>(cap);","handlingStrategy":"validation","validationCode":"int cap = Math.max(1, configuredCapacity);\nThreadSafeQueue<T> q = new ThreadSafeQueue<>(cap);","typeGuard":"boolean validCapacity = configuredCapacity > 0;","tryCatchPattern":"null","preventionTips":["Validate capacity at the config boundary; capacity feeds the ring buffer and the modulo arithmetic.","Clamp computed capacities to at least 1.","Treat a non-positive capacity as a configuration error and fail fast with context."],"tags":["queue","data-structure","concurrency","constructor","capacity","input-validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}