-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathCallsToRunnableRun.qhelp
More file actions
64 lines (49 loc) · 2.26 KB
/
CallsToRunnableRun.qhelp
File metadata and controls
64 lines (49 loc) · 2.26 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
58
59
60
61
62
63
64
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>A direct call of a <code>Thread</code> object's <code>run</code> method
does not start a separate thread. The method is executed within the current thread.
This is an unusual use because <code>Thread.run()</code> is normally
intended to be called from within a separate thread.
</p>
</overview>
<recommendation>
<p>To execute <code>Runnable.run</code> from within a separate thread, do one of the
following:</p>
<ul>
<li>Construct a <code>Thread</code> object using the <code>Runnable</code> object, and call
<code>start</code> on the <code>Thread</code> object.</li>
<li>Define a subclass of a <code>Thread</code> object, and override the definition of its
<code>run</code> method. Then construct an instance of this subclass and call <code>start</code>
on that instance directly.</li>
</ul>
</recommendation>
<example>
<p>In the following example, the main thread, <code>ThreadDemo</code>, calls the child thread,
<code>NewThread</code>, using <code>run</code>. This causes the child thread to run to
completion before the rest of the main thread is executed, so that "Child thread activity" is
printed before "Main thread activity".</p>
<sample src="CallsToRunnableRun.java" />
<p>To enable the two threads to run concurrently, create the child thread and call
<code>start</code>, as shown below. This causes the main thread to
continue while the child thread is waiting, so that "Main thread activity" is printed before
"Child thread activity".</p>
<sample src="CallsToRunnableRunFixed.java" />
</example>
<references>
<li>
The Java Tutorials: <a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html">Defining and Starting a Thread</a>.
</li>
<li>
SEI CERT Oracle Coding Standard for Java: <a href="https://wiki.sei.cmu.edu/confluence/display/java/THI00-J.+Do+not+invoke+Thread.run()">THI00-J. Do not invoke Thread.run()</a>.
</li>
<li>
Java API Specification: <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Thread.html">Thread</a>.
</li>
<li>
Java API Specification: <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Runnable.html">Runnable</a>.
</li>
</references>
</qhelp>