-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathStartInConstructor.qhelp
More file actions
49 lines (35 loc) · 1.53 KB
/
StartInConstructor.qhelp
File metadata and controls
49 lines (35 loc) · 1.53 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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Starting a thread within a constructor may cause unexpected results. If the class is extended,
the thread may start before the subclass constructor has completed its initialization,
which may not be intended.</p>
</overview>
<recommendation>
<p>Avoid starting threads in constructors.
Typically, the constructor of a class only <i>constructs</i> the thread object,
and a separate <code>start</code> method should be provided to <i>start</i> the thread object
created by the constructor.
</p>
</recommendation>
<example>
<p>In the following example, because the <code>Test</code> constructor implicitly calls the
<code>Super</code> constructor, the thread created in the <code>Super</code> constructor may start
before <code>this.name</code> has been initialized. Therefore, the program may
output "hello " followed by a null string.
</p>
<sample src="StartInConstructor.java" />
<p>In the following modified example, the thread created in the <code>Super</code> constructor is not started
within the constructor; <code>main</code> starts the thread after <code>this.name</code> has been
initialized. This results in the program outputting "hello my friend".</p>
<sample src="StartInConstructorGood.java" />
</example>
<references>
<li>
IBM developerWorks:
<a href="https://web.archive.org/web/20200417101823/http://www.ibm.com/developerworks/java/library/j-jtp0618/index.html#4">Don't start threads from within constructors</a>.
</li>
</references>
</qhelp>