Thread Subclass Must Override run()
ID |
java.thread_subclass_must_override_run |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
concurrency, reliability |
Description
Reports classes that extend Thread without overriding the run() method. A Thread subclass that does not override run() executes the empty default implementation, making the thread effectively a no-op.
Rationale
When a class extends Thread, the intent is to define the thread’s behavior by overriding run(). If run() is not overridden, calling start() creates a new OS thread that immediately terminates without doing any work. This is almost always a bug or an oversight. Abstract classes are excluded because they may intentionally delegate run() to a concrete subclass.
// Bad -- the thread does nothing
class WorkerThread extends Thread {
private final String name;
WorkerThread(String name) {
this.name = name;
}
// Missing run() override!
}
Remediation
Override run() to define the thread’s behavior, or use Runnable instead of subclassing Thread:
// Option 1: override run()
class WorkerThread extends Thread {
@Override
public void run() {
doWork();
}
}
// Option 2: prefer Runnable (more flexible)
Thread t = new Thread(() -> doWork());
t.start();