-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathEmptyRunMethodInThread.qhelp
More file actions
57 lines (40 loc) · 1.72 KB
/
EmptyRunMethodInThread.qhelp
File metadata and controls
57 lines (40 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>New threads can be defined using one of the following alternatives:</p>
<ul>
<li>By extending the <code>Thread</code> class and overriding its <code>run</code> method.</li>
<li>By passing an argument of type <code>Runnable</code> to the constructor of the <code>Thread</code> class.</li>
</ul>
<p>Thread instances that are defined using another approach are likely to have no effect.</p>
</overview>
<recommendation>
<p>To avoid empty thread instances, define new threads using one of the following alternatives:</p>
<ul>
<li>By extending the <code>Thread</code> class and overriding its <code>run</code> method.</li>
<li>By passing an argument of type <code>Runnable</code> to the constructor of the
<code>Thread</code> class.</li>
</ul>
</recommendation>
<example>
<p>In the following example, class <code>Bad</code> shows the definition of a thread that has no effect.</p>
<sample src="EmptyRunMethodInThread.java" />
<p>In the following example, class <code>GoodWithOverride</code> shows how to extend the
<code>Thread</code> class and override its <code>run</code> method, and class
<code>GoodWithRunnable</code> shows how to pass an argument of type <code>Runnable</code> to the
constructor of the <code>Thread</code> class.
</p><sample src="EmptyRunMethodInThreadGood.java" />
</example>
<references>
<li>
Java API Specification:
<a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Thread.html">Thread</a>.
</li>
<li>
The Java Tutorials:
<a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html">Defining and Starting a Thread</a>.
</li>
</references>
</qhelp>