Z Score,T Score, Percential Rank and Box Plot Graph
Multi Threading.docx
1. A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing
and multithreading, both are used to achieve multitasking.
However, we use multithreading than multiprocessing because threads use a shared
memory area. They don't allocate separate memory area so saves memory, and context-
switching between the threads takes less time than process.
Advantages:
1) It doesn't block the user because threads are independent and you can perform
multiple operations at the same time.
2) You can perform many operations together, so it saves time.
3) Threads are independent, so it doesn't affect other threads if an exception occurs in
a single thread.
Multitasking
Multitasking is a process of executing multiple tasks simultaneously. We use multitasking
to utilize the CPU. Multitasking can be achieved in two ways:
o Process-based Multitasking (Multiprocessing)
o Thread-based Multitasking (Multithreading)
1) Process-based Multitasking (Multiprocessing)
o Each process has an address in memory. In other words, each process allocates a
separate memory area.
o A process is heavyweight.
o Cost of communication between the process is high.
o Switching from one process to another requires some time for saving and loading
registers, memory maps, updating lists, etc.
2) Thread-based Multitasking (Multithreading)
o Threads share the same address space.
o A thread is lightweight.
o Cost of communication between the thread is low.
In computing, a process is an instance of a computer program that
is being executed.
2. Any process has 3 basic components:
1.An executable program.
2.The associated data needed by the program (variables, work space,
buffers, etc.)
3.The execution context of the program (State of process)
A thread is an entity within a process that can be scheduled
for execution. Also, it is the smallest unit of processing that
can be performed in an OS (Operating System).
In simple words, a thread is a sequence of such instructions
within a program that can be executed independently of other code.
For simplicity, you can assume that a thread is simply a subset
of a process!
import threading
def print_cube(num):
"""
function to print cube of given num
3. """
print("Cube: {}".format(num * num * num))
def print_square(num):
"""
function to print square of given num
"""
print("Square: {}".format(num * num))
if __name__ == "__main__":
# creating thread
t1 = threading.Thread(target=print_square, args=(10,))
t2 = threading.Thread(target=print_cube, args=(10,))
# starting thread 1
t1.start()
# starting thread 2
t2.start()
# wait until thread 1 is completely executed
t1.join()
# wait until thread 2 is completely executed
t2.join()
# both threads completely executed
print("Done!")
To create a new thread, we create an object of Thread class. It takes following
arguments:
target: the function to be executed by thread
args: the arguments to be passed to the target function
In above example, we created 2 threads with different target functions:
t1 = threading.Thread(target=print_square, args=(10,))
t2 = threading.Thread(target=print_cube, args=(10,))
To start a thread, we use start method of Thread class.
t1.start()
t2.start()
Once the threads start, the current program (you can think of it like a main
thread) also keeps on executing. In order to stop execution of current program
until a thread is complete, we use join method.
t1.join()
t2.join()
As a result, the current program will first wait for the completion of t1 and
then t2. Once, they are finished, the remaining statements of current program
are executed
import threading
import time
4. def square(num):
print("calculate square numbers:")
for n in num:
time.sleep(0.2)
print("square:",n*n)
def cube(num):
print("calculate cube numbers:")
for n in num:
time.sleep(0.2)
print("cube:", n * n*n)
arr=[2,3,4,5]
t=time.time()
'''t1=threading.Thread(target=square,ar
gs=(arr,))
t2=threading.Thread(target=cube,args=(a
rr,))
t1.start()
t2.start()
t1.join()
t2.join()'''
square(arr)
cube(arr)