{"record":{"id":"6ae23ff302aee915","repo":"crossoverJie/JCSprout","slug":"counter-0","errorCode":null,"errorMessage":"counter < 0","messagePattern":"counter < 0","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/crossoverjie/concurrent/communication/MultipleThreadCountDownKit.java","lineNumber":28,"sourceCode":" * @since JDK 1.8\n */\npublic final class MultipleThreadCountDownKit {\n\n    /**\n     * 计数器\n     */\n    private AtomicInteger counter;\n\n    /**\n     * 通知对象\n     */\n    private Object notify ;\n\n    private Notify notifyListen ;\n\n    public MultipleThreadCountDownKit(int number){\n        if (number < 0) {\n            throw new IllegalArgumentException(\"counter < 0\");\n        }\n        counter = new AtomicInteger(number) ;\n        notify = new Object() ;\n    }\n\n    /**\n     * 设置回调接口\n     * @param notify\n     */\n    public void setNotify(Notify notify){\n        notifyListen = notify ;\n    }\n\n\n    /**\n     * 线程完成后计数 -1\n     */\n    public void countDown(){","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/crossoverJie/JCSprout/blob/fc4c6e5f6d1772c2aec4c0f94cb3c8e7eb04018f/src/main/java/com/crossoverjie/concurrent/communication/MultipleThreadCountDownKit.java#L10-L46","documentation":"The constructor of MultipleThreadCountDownKit (a custom CountDownLatch) rejects a negative initial count with IllegalArgumentException. A countdown latch needs a non-negative starting counter; a negative value has no meaningful semantics for tracking how many threads must report completion.","triggerScenarios":"Constructing new MultipleThreadCountDownKit(-1) or any negative int. Note that 0 IS permitted — it means all work is already considered complete, so await() returns immediately and countDown() is a no-op.","commonSituations":"Computing the count from a subtraction or list-size that can go negative (e.g. new MultipleThreadCountDownKit(total - reserved) where reserved > total). Passing an unvalidated value parsed from configuration or a request parameter.","solutions":["Validate the count before construction and reject or clamp it in your calling code.","If the count can legitimately be zero, guard with Math.max(0, computedCount).","Review the arithmetic that produces the count to ensure it cannot go negative under any input."],"exampleFix":"// before\nnew MultipleThreadCountDownKit(totalTasks - reservedSlots); // negative if reserved > total\n\n// after\nint count = Math.max(0, totalTasks - reservedSlots);\nnew MultipleThreadCountDownKit(count);","handlingStrategy":"validation","validationCode":"if (number < 0) {\n    throw new IllegalArgumentException(\"count must be >= 0, got \" + number);\n}\nMultipleThreadCountDownKit latch = new MultipleThreadCountDownKit(number);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate the count at the call site before constructing the latch.","Use Math.max(0, computedCount) when zero is an acceptable degenerate case.","Audit all arithmetic that produces the count to ensure it cannot go negative."],"tags":["countdown","validation","constructor"],"backgroundTag":null,"analyzedSha":"fc4c6e5f6d1772c2aec4c0f94cb3c8e7eb04018f","analyzedAt":"2026-08-14T05:43:20.992Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}