-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreading.py
More file actions
235 lines (184 loc) · 5.75 KB
/
MultiThreading.py
File metadata and controls
235 lines (184 loc) · 5.75 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
# WHAT IS MULTI-TASKING ?
'''
- The process of multi-tasking lets a CPU execute various tasks at the very same time.
- CPU switching occurs very often when multitasking between various tasks. Since it involves rapid CPU switching, it
requires some time. It is because switching from one user to another might need some resources.
'''
# WHAT IS MULTI-THREADING ?
'''
- It is a system that creates many threads out of a single process to increase the overall power and working capacity
of a computer.
- In the process of multi-threading, we use a CPU for executing many threads out of a single process at any given time.
- Thread is subset of process or we can say thread is a light weight process or we can say thread nothing but part of
process.
- Default thread is a main thread because it is responsible to execute the process.
- Dependancy is not allowed in multi-threading.
'''
# When to use Multi-Threading ?
'''
- It is a very useful technique for time-saving and improving the performance of an application.
- Multithreading allows the programmer to divide application tasks into sub-tasks and simultaneously run them in a program.
- It allows threads to communicate and share resources such as files, data, and memory to the same processor.
'''
# Benefits of Multi-Threading
'''
1. It ensures effective utilization of computer system resources.
2. It saves time by executing multiple threads at the same time.
3. The system does not require too much memory to store multiple threads.
'''
# JOIN() method:
'''
- It is a join() method used in the thread class to halt the main thread's execution and waits till the complete
execution of the thread object. When the thread object is completed, it starts the execution of the main thread in Python.
- A join() method is used to block the execution of another code until the thread terminates.
'''
# START() method:
'''
- To start a thread in Python multithreading, call the thread class's object. The start() method can be called once for
each thread object; otherwise, it throws an exception error.
- A start() method is used to initiate the activity of a thread. And it calls only once for each thread so that the
execution of the thread can begin.
'''
# 1. CREATING THREAD WITHOUT USING ANY CLASS (function base threading)
'''
import threading
print(threading.current_thread())
print(threading.current_thread().name)
print(threading.main_thread())
print(threading.main_thread().name)
def add():
a = 5
b = 50
print(a+b)
add()
print("Addition Done")
'''
# EXAMPLE: 1
'''
import threading
def add():
a = 5
b = 50
print(a+b)
# add()
print("Addition Done")
t1 = threading.Thread(target=add())
t1.start()
print("Threading Operation Done")
'''
# EXAMPLE: 2
'''
import threading
def add():
a = 5
b = 50
print(a+b)
for i in range(5):
print("Hii")
t1 = threading.Thread(target=add())
t1.start()
for j in range(5):
print("Bye")
'''
# EXAMPLE: 3
'''
import threading
def add():
a = 5
b = 50
print(a+b)
for i in range(5):
print("Hii")
def C1():
print("Check")
t1 = threading.Thread(target=add())
t1.start()
t2 = threading.Thread(target=C1())
t2.start()
for j in range(5):
print("Bye")
'''
# EXAMPLE: 3
'''
import threading
def add():
a = 5
b = 50
print(a+b)
for i in range(5):
print("Hii")
t1 = threading.Thread(target=add())
t1.start()
def C1():
print("Check")
t2 = threading.Thread(target=C1())
t2.start()
for j in range(5):
print("Bye")
'''
# ○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○
# 2. CREATING THREAD BY EXTENDING THREAD CLASS
# EXAMPLE: 1
'''
from threading import Thread
class Check(Thread):
def run(self):
for i in range(5):
print("Hii")
t1 = Check()
t1.start()
for i in range(10):
print("Bye")
'''
# ○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○
# 3. CREATING THREAD WITHOUT EXTENDING THREAD CLASS
# EXAMPLE: 1
'''
from threading import Thread
class Check:
def m1(self):
for i in range(5):
print("Child")
def m2(self):
for j in range(10):
print("Parent")
obj = Check()
a = Thread(target=obj.m1())
b = Thread(target=obj.m2())
'''
# EXAMPLE: 2
'''
from threading import Thread
class Check:
def m1(self):
for i in range(5):
print("Child")
def m2(self):
for j in range(10):
print("Parent")
obj = Check()
t1 = Thread(target=obj.m1())
t2 = Thread(target=obj.m2())
t1.start()
t2.start()
'''
# EXAMPLE: 3 -With Multi-Threading
'''
from threading import Thread
import time
class Check:
def m1(self):
for i in range(5):
print("Child")
time.sleep(1)
def m2(self):
for j in range(5):
print("Parent")
time.sleep(1)
StartTime = time.time()
obj = Check()
t1 = Thread(target=obj.m1())
t2 = Thread(target=obj.m2())
print("Total time required: ", time.time()-StartTime, "sec")
'''
# ☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻