55 lines
1.2 KiB
Plaintext
55 lines
1.2 KiB
Plaintext
While it is technically correct to use a ``++Thread++`` where a ``++Runnable++`` is called for, the semantics of the two objects are different, and mixing them is a bad practice that will likely lead to headaches in the future.
|
|
|
|
|
|
The crux of the issue is that ``++Thread++`` is a larger concept than ``++Runnable++``. A ``++Runnable++`` is an object whose running should be managed. A ``++Thread++`` expects to manage the running of itself or other ``++Runnables++``.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
[source,java]
|
|
----
|
|
public static void main(String[] args) {
|
|
Thread r =new Thread() {
|
|
int p;
|
|
@Override
|
|
public void run() {
|
|
while(true)
|
|
System.out.println("a");
|
|
}
|
|
};
|
|
new Thread(r).start(); // Noncompliant
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
[source,java]
|
|
----
|
|
public static void main(String[] args) {
|
|
Runnable r =new Runnable() {
|
|
int p;
|
|
@Override
|
|
public void run() {
|
|
while(true)
|
|
System.out.println("a");
|
|
}
|
|
};
|
|
new Thread(r).start();
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|