2023-06-02 15:54:01 +02:00
|
|
|
This rule raises an issue when `Thread.run()` is called instead of `Thread.start()`.
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2023-06-02 15:54:01 +02:00
|
|
|
The likely intention of a user calling `Thread.run()` is to start the execution of code within a new thread.
|
|
|
|
This, however, is not what happens when this method is called.
|
|
|
|
|
|
|
|
The purpose of `Thread.run()` is to provide a method that users can overwrite to specify the code to be executed.
|
|
|
|
The actual thread is then started by calling `Thread.start()`.
|
|
|
|
When `Thread.run()` is called directly, it will be executed as a regular method within the current thread.
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-06-02 15:54:01 +02:00
|
|
|
== How to fix it
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-06-02 15:54:01 +02:00
|
|
|
If you intend to execute the contents of the `Thread.run()` method within a new thread,
|
|
|
|
call `Thread.start()` instead.
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-06-02 15:54:01 +02:00
|
|
|
If your intention is only to have a container for a method but execute this method within the current thread,
|
|
|
|
do not use `Thread` but `Runnable` or another functional interface.
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-06-02 15:54:01 +02:00
|
|
|
=== Code examples
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-06-02 15:54:01 +02:00
|
|
|
==== Noncompliant code example
|
|
|
|
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
Thread myThread = new Thread(runnable);
|
2023-06-02 15:54:01 +02:00
|
|
|
myThread.run(); // Noncompliant, does not start a thread
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
|
2023-06-02 15:54:01 +02:00
|
|
|
==== Compliant solution
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-06-02 15:54:01 +02:00
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
Thread myThread = new Thread(runnable);
|
|
|
|
myThread.start(); // Compliant
|
|
|
|
----
|
|
|
|
|
2023-06-02 15:54:01 +02:00
|
|
|
==== Noncompliant code example
|
|
|
|
|
|
|
|
[source,java,diff-id=2,diff-type=noncompliant]
|
|
|
|
----
|
|
|
|
class ComputePrimesThread extends Thread {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new ComputePrimesThread().run(); // Noncompliant, does not start a thread
|
|
|
|
----
|
|
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
[source,java,diff-id=2,diff-type=compliant]
|
|
|
|
----
|
|
|
|
class ComputePrimesThread extends Thread {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new ComputePrimesThread().start(); // Compliant
|
|
|
|
----
|
|
|
|
|
|
|
|
==== Noncompliant code example
|
|
|
|
|
|
|
|
[source,java,diff-id=3,diff-type=noncompliant]
|
|
|
|
----
|
|
|
|
class Button {
|
|
|
|
|
|
|
|
private Thread onClick;
|
|
|
|
|
|
|
|
Button(Thread onClick) {
|
|
|
|
this.onClick = onClick;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void clicked() {
|
|
|
|
if (onClick != null) onClick.run(); // Noncompliant, use functional interface
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new Button(new Thread() {
|
|
|
|
@Override public void run() {
|
|
|
|
System.out.println("clicked!");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
----
|
|
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
[source,java,diff-id=3,diff-type=compliant]
|
|
|
|
----
|
|
|
|
class Button {
|
|
|
|
|
|
|
|
private Runnable onClick;
|
|
|
|
|
|
|
|
Button(Runnable onClick) {
|
|
|
|
this.onClick = onClick;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void clicked() {
|
|
|
|
if (onClick != null) onClick.run(); // compliant
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new Button(() -> System.out.println("clicked!"));
|
|
|
|
----
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
== Resources
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2023-06-02 15:54:01 +02:00
|
|
|
=== Documentation
|
|
|
|
|
|
|
|
* https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#start--[Java™ Platform, Standard Edition 8 API Specification - Thread.start()]
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-06-02 15:54:01 +02:00
|
|
|
=== Articles & blog posts
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-06-02 15:54:01 +02:00
|
|
|
* https://www.javatpoint.com/what-if-we-call-java-run-method-directly[JavaTPoint - What if we call Java run() method directly instead start() method?]
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
Call the method Thread.start() to execute the content of the run() method in a dedicated thread.
|
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== on 20 Aug 2013, 15:40:32 Freddy Mallet wrote:
|
|
|
|
Is implemented by \http://jira.codehaus.org/browse/SONARJAVA-308
|
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|