rspec/rules/S2438/java/rule.adoc

64 lines
1.5 KiB
Plaintext

== Why is this an issue?
The semantics of `Thread` and `Runnable` are different, and while it is technically correct to use `Thread` where a `Runnable` is expected, it is a bad practice to do so.
The crux of the issue is that `Thread` is a larger concept than `Runnable`.
A `Runnable` represents a task.
A `Thread` represents a task and its execution management (ie: how it should behave when started, stopped, resumed, ...).
It is both a task and a lifecycle management.
=== Noncompliant code example
[source,java,diff-id=1,diff-type=noncompliant]
----
public static void main(String[] args) {
Thread runnable = new Thread() {
@Override
public void run() { /* ... */ }
};
new Thread(runnable).start(); // Noncompliant
}
----
=== Compliant solution
[source,java,diff-id=1,diff-type=compliant]
----
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() { /* ... */ }
};
new Thread(runnable).start();
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Argument n is a "Thread".
* "XXX" is a "Thread".
'''
== Comments And Links
(visible only on this page)
=== on 20 Jan 2015, 16:20:05 Ann Campbell wrote:
\[~nicolas.peru] I'd like to add a simile to illustrate the issue. What do you think of this?
____
It would be something like asking Sir Edmund Hillary to take a hike up the nearest hill with you.
____
endif::env-github,rspecator-view[]