Invoking a `Runnable` object's `run` method will perform the task in the current thread, not a new oneJAVA-W0135
The run
method of the referenced Thread
was invoked. Did you mean to invoke start
instead?
This code explicitly invokes run
on an object. In general, classes implement the Runnable
interface because they are going to have their run
method invoked in a new thread, in which case Thread.start
is the right method to call.
Calling Runnable.run
directly will execute code meant to run on a separate thread on the same thread, blocking execution and breaking any code that expects the contents of the run
method to be executed on a different thread.
Thread a = new Thread(new Runnable() {
@Override
public void run() { ... }
});
Bad Practice
a.run(); // Will not spawn a new thread!
Recommended
a.start(); // Will spawn a new thread.
a.join(); // And this works as expected.
References
- Spotbugs - RU_INVOKE_RUN