Thread Subclass Must Override Run

ID

kotlin.thread_subclass_must_override_run

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Kotlin

Tags

concurrency, reliability

Description

Reports concrete classes that extend Thread without overriding run(). The Thread.run() inherited from the JDK does nothing, so the spawned OS thread simply starts and exits.

Rationale

class Worker : Thread()       // FLAW — no run() override

fun main() {
    Worker().start()          // starts a thread that immediately exits
}

A subclass of Thread exists to attach behaviour to a thread. If run() isn’t overridden, the subclass adds nothing — passing a Runnable lambda to the Thread constructor would be both shorter and easier to read.

Remediation

Either override run() with the actual work:

class Worker : Thread() {
    override fun run() {
        println("working")
    }
}

Or stop subclassing Thread and pass a lambda:

val t = Thread { println("working") }
t.start()

Abstract subclasses are excluded because they may defer run() to a concrete sub-subclass.

References