114 lines
3.4 KiB
Plaintext
114 lines
3.4 KiB
Plaintext
== Why is this an issue?
|
|
|
|
The default implementation of `java.lang.Thread` 's `run` will only perform a task passed as a `Runnable`.
|
|
If no `Runnable` has been provided at construction time, then the thread will not perform any action.
|
|
|
|
When extending `java.lang.Thread`, you should override the `run` method or pass a `Runnable` target to the constructor of `java.lang.Thread`.
|
|
|
|
=== Noncompliant code example
|
|
[source,java]
|
|
----
|
|
public class MyThread extends Thread { // Noncompliant
|
|
public void doSomething() {
|
|
System.out.println("Hello, World!");
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== How to fix it
|
|
To fix this issue, you have 2 options:
|
|
|
|
* override the `run` method
|
|
|
|
[source,java]
|
|
----
|
|
public class MyThread extends Thread {
|
|
@Override
|
|
public void run() {
|
|
System.out.println("Hello, World!");
|
|
}
|
|
}
|
|
----
|
|
|
|
* provide a `Runnable` at construction time
|
|
|
|
[source,java]
|
|
----
|
|
public class MyRunnable implements Runnable {
|
|
@Override
|
|
public void run() {
|
|
System.out.println("Hello, World!");
|
|
}
|
|
}
|
|
public class MyThread extends Thread {
|
|
public MyThread(Runnable runnable) {
|
|
super(runnable);
|
|
}
|
|
}
|
|
|
|
public class Main() {
|
|
public static void main(String [] args) {
|
|
Runnable runnable = new MyRunnable();
|
|
Thread customThread = new MyThread(runnable);
|
|
Thread regularThread = new Thread(runnable);
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Don't extend "Thread", since the "run" method is not overridden.
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 10 Oct 2014, 11:14:05 Freddy Mallet wrote:
|
|
My 2 cents @Ann, I would:
|
|
|
|
* Replace the tag "performance" by "bug"
|
|
* Replace the category "Efficiency" by "Reliability"
|
|
|
|
Moreover, I would recommend the following remediation action:
|
|
|
|
* MakeMyThread
|
|
r directly implements the Runnable interface
|
|
* Then create and launch a thread like this : "new Thread(newMyThread
|
|
r()).start()"
|
|
|
|
=== on 10 Oct 2014, 18:25:43 Ann Campbell wrote:
|
|
\[~freddy.mallet] I've implemented your first 2 suggestions, but the last 2 confused me.
|
|
|
|
|
|
I think the emphasis of this rule was itself confused. The title addressed the `Runnable` but the description was about the class that created an instance of a vacuous `Runnable`. I've updated the description and code samples to move the emphasis to the empty `Runnable`. Please let me know if you feel changes are still needed.
|
|
|
|
=== on 11 Oct 2014, 12:23:51 Freddy Mallet wrote:
|
|
\[~ann.campbell.2], there are two ways to implement a thread :
|
|
|
|
____
|
|
One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started
|
|
|
|
The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started
|
|
|
|
____
|
|
|
|
In term of separation of concerns, the second approach is definitely the best one. Moreover with this second approach it's simply impossible to forget to implement the run() method whereas when extending the Thread class, nothing compel you to override the Thread.run() method.
|
|
|
|
=== on 14 Oct 2014, 20:25:42 Freddy Mallet wrote:
|
|
I'm sorry @Ann, but I'm going to rework this rule.
|
|
|
|
=== on 12 Jun 2015, 15:18:36 Ann Campbell wrote:
|
|
CodePro: No Run Method
|
|
|
|
endif::env-github,rspecator-view[]
|