All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.squid.S2438.html Maven / Gradle / Ivy

There is a newer version: 8.6.0.37351
Show newest version

While it is technically correct to use a Thread where a Runnable is called for, the semantics of the two objects are different, and mixing them is a bad practice that will likely lead to headaches in the future.

The crux of the issue is that Thread is a larger concept than Runnable. A Runnable is an object whose running should be managed. A Thread expects to manage the running of itself or other Runnables.

Noncompliant Code Example

    public static void main(String[] args) {
        Thread r =new Thread() {
            int p;
            @Override
            public void run() {
                while(true)
                    System.out.println("a");
            }
        };
        new Thread(r).start();  // Noncompliant

Compliant Solution

    public static void main(String[] args) {
        Runnable r =new Runnable() {
            int p;
            @Override
            public void run() {
                while(true)
                    System.out.println("a");
            }
        };
        new Thread(r).start();




© 2015 - 2025 Weber Informatics LLC | Privacy Policy