Do Not Call Thread.run() Directly
ID |
java.thread_run_called_directly |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
concurrency, reliability |
Description
Reports direct calls to Thread.run(). Calling run() executes the method in the caller’s thread instead of starting a new thread.
Rationale
Thread.run() simply executes the Runnable’s `run() method on the current thread. The intended way to start a thread is via Thread.start(), which creates a new OS-level thread and then invokes run() on it. Calling run() directly defeats the entire purpose of creating a Thread object.
// Bad -- runs in the current thread, not a new thread
Thread t = new Thread(() -> doWork());
t.run();
Remediation
Use start() to actually spawn a new thread:
// Good -- starts a new thread
Thread t = new Thread(() -> doWork());
t.start();