code4craft/webmagic · error · IllegalArgumentException

threadNum should be more than one!

Error message

threadNum should be more than one!

What it means

Thrown by Spider.thread(int) when the requested worker-thread count is zero or negative. It is a generic validation guard on the caller-supplied threadNum parameter: the value must be at least 1 for the spider to spawn workers, so any non-positive input passed to thread() triggers this IllegalArgumentException immediately, before threadNum is applied.

Solutions

  1. Pass a threadNum of at least 1
  2. Validate/parse the config value before calling thread() and default to 1 when invalid
  3. Check the source of the value for typos or missing keys

Example fix

// before
int n = Integer.parseInt(props.getProperty("spider.threads", "0"));
spider.thread(n);
// after
int n = Math.max(1, Integer.parseInt(props.getProperty("spider.threads", "1")));
spider.thread(n);
Defensive patterns

Strategy: validation

Validate before calling

int n = Integer.parseInt(props.getProperty("spider.threads", "1")); if (n <= 0) n = 1; spider.thread(n);

Try / catch

try { spider.thread(n); } catch (IllegalArgumentException e) { spider.thread(1); }

Prevention

When it happens

Trigger: Calling spider.thread(0) or spider.thread(-1) before running; computing the thread count from a config/property that defaults to 0.

Common situations: Loading thread count from a properties/yaml file that is missing or empty and parsed as 0; passing an uninitialized int field.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of code4craft/webmagic@67816a19d6 (2026-09-08). Data as JSON: /api/errors/3cf2258d647c1df2. Report an issue: GitHub.

Appendix: source

Thrown at webmagic-core/src/main/java/us/codecraft/webmagic/Spider.java:617

    /**
     * Stop when all tasks in the queue are completed and all worker threads are also completed
     */
    public void stopWhenComplete(){
        this.exitWhenComplete = true;
    }

    /**
     * start with more than one threads
     *
     * @param threadNum threadNum
     * @return this
     */
    public Spider thread(int threadNum) {
        checkIfRunning();
        this.threadNum = threadNum;
        if (threadNum <= 0) {
            throw new IllegalArgumentException("threadNum should be more than one!");
        }
        return this;
    }

    /**
     * start with more than one threads
     *
     * @param executorService executorService to run the spider
     * @param threadNum threadNum
     * @return this
     */
    public Spider thread(ExecutorService executorService, int threadNum) {
        checkIfRunning();
        this.threadNum = threadNum;
        if (threadNum <= 0) {
            throw new IllegalArgumentException("threadNum should be more than one!");
        }
        this.executorService = executorService;

View on GitHub (pinned to 67816a19d6)