Skip to content

[java] DoNotUseThreads: False positive on legitimate java.lang.Thread.onSpinWait() call #6520

@leemeii

Description

@leemeii

Affects PMD Version: 7.22.0

Rule:DoNotUseThreads

Description:
The DoNotUseThreads rule is intended to prevent applications from creating or managing their own threads/ExecutorServices (as required by the J2EE specification).
However, it incorrectly triggers on the static method call java.lang.Thread.onSpinWait(), which is not thread creation or management — it is a performance hint (intrinsic) introduced in Java 9 to help the JIT optimize spin-wait loops. This call appears in official OpenJDK jtreg tests and many legitimate performance-critical codes.

Code Sample demonstrating the issue:

public class a{
    public void foo(){
        java.lang.Thread.onSpinWait();
    }
}

Expected outcome:

PMD reports a violation at line 3 for DoNotUseThreads ("To be compliant to J2EE, a webapp should not use any thread."), but that's wrong. That's a false positive.

Running PMD through: CLI

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions