-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultithreading.html
More file actions
238 lines (209 loc) · 9.62 KB
/
multithreading.html
File metadata and controls
238 lines (209 loc) · 9.62 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!--
~ Author: @tridib2003
~ Repository: https://github.com/tridib2003/levelupjava
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multithreading | Level-Up Java</title>
<link href="styles.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<nav class="navigation-std">
<ul class="ul-remove-bullet nav-pills">
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Previous</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Home</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Next</a>
</li>
</ul>
</nav>
<h1 class="h1-basic">Multithreading</h1>
<div class="desc-container desc-container-center desc-std">
<hr>
<ul>
<li>
<p>
<strong><i>Multitasking</i></strong> is the capabity to execute two or more programs (processes)
concurrently. Whereas, <strong><i>multithreading</i></strong> is the capability when a single
program can perform two or more tasks concurrently, thus making maximum use of the processing power
available in the system.
</p>
</li>
<li>
<p>
Multitasking threads require less overhead than Multitasking processes.
<ul>
<li>
<p>
<i>Processes</i> are heavy-weight tasks that require their own seperate adderess spaces,
whereas <i>threads</i> are light-weight and share the same address space and
cooperatively
share the same heavy-weight process.
</p>
</li>
<li>
<p>
Interprocess communication is costly, whereas interthread communication is inexpensive.
</p>
</li>
<li>
<p>
Context switching from one process to another is also costly, whereas in case of
threads,
context switching is lower in cost.
</p>
</li>
</ul>
</p>
</li>
<li>
<p>
The benefit of <i>multithreading</i> is that while one thread pauses for some parts of the program
to finish executing, like waiting for user input etc., the idle time created can be utilized
elsewhere, thus all other threads continue to execute.
</p>
</li>
<li>
<p>
A <i>thread</i> lifecycle has several states. Once a thread is created and gets CPU time it can be
<i>ready to run</i>. A thread can be <i>running</i>. A running thread can be <i>suspended</i>, which
temporarily stops executing. A suspended thread can be <i>resumed</i>, from where it stopped. A
thread can also be <i>blocked</i>, when its not getting a resource. Anytime a thread can be
<i>terminated</i> and cannot resume execution thereafter.
</p>
<p title="Throwable class hierarchy" style="text-align:center">
<img src="assets/images/LUJ-MT-01.png" alt="Throwable class hierarchy" width=500
class="img-responsive" />
</p>
</li>
<li>
<p>
Threads are assigned a <i>priority</i>, basically an integer value that determines when to switch
from one running thread to the next. This is known as <i>context switch</i>. A thread can
voluntarily give up control or can be preempted by a higher-priority thread.
</p>
</li>
<li>
<p>
If a thread wants to modify some data where another thread is in the middle of reading it, such
situation needs to be prevented. In Java, synchronization support is built into the language. When a
thread is inside a synchronized method, no other thead can call any other synchronized method on the
same object.
</p>
</li>
<li>
<p>
The <i>main</i> thread starts running immediately when a program is executed and generally it is the
last thread to finish execution because it performs various shutdown actions. Also, from the
<i>main</i> thread other <i>child</i> threads are spawned.
</p>
</li>
<li>
<p>
A thread can be created by instantiating an object of type <strong>Thread</strong>. It can be done
either by implementing the <strong>Runnable</strong> interface, or by extending the
<strong>Thread</strong> class.
</p>
</li>
<li>
<p>
<strong>Runnable</strong> can be implemented by implementing a single method called
<strong>run()</strong>. It specifies the entry point for another, concurrent thread of execution
withing the program. Inside <strong>run()</strong> that code that constitute the new thread is
written. After a new thread is created, to make it running, call the <strong>start()</strong>
method, which initiates a call to <strong>run()</strong>.
</p>
</li>
<li>
<p>
When creating a new thread by extending the <strong>Thread</strong> class and then creating an
instance of that class, the extending class must override the <strong>run()</strong> method which
serves as the entry point for the new thread. A call to <strong>start()</strong> begins execution of
the new thread.
</p>
</li>
</ul>
</div>
<br><br><br>
<div class="table-container table-container-center">
<table class="table-code-links">
<tr>
<td>
Exploring the main thread
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Multithreading/MainThreadDemo.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
<tr>
<td>
Create child thread by implementing Runnable
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Multithreading/MultithreadingDemo.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
<tr>
<td>
Create child thread by extending Thread
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Multithreading/MultithreadingDemo2.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
</table>
</div>
<br><br><br>
<footer class="navigation-std">
<ul class="ul-remove-bullet nav-pills">
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Previous</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Home</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Next</a>
</li>
</ul>
</footer>
</body>
</html>