Python Online Training In Andhra Pradesh and Telangana
Opening Hours :7AM to 9PM
Without Using Threading Rules:
Program: Display 1-10 number with 2sec time gap
counter =counter+ 1
Thread is a flow of execution. In Python language these Threads are classified into two types 1.Single Tasking(threading) 2.Multitasking(threading)
Whenever processor executing only one task (operation) is known as single tasking. The main disadvantage with single tasking is utilization of processor time is not done properly to overcome this problem multi threading was introduced.
Whenever multiple tasks are executing simultaneously by the processor is known as multitasking. Multitasking is classified into two ways.
1.Process based Multi Tasking
2.Thread based Multi Tasking
Process based multi threading or multi tasking:
Whenever multiple processor of different applications are running simultaneously to perform different operation by the processor is known as process based multitasking. Example:
playing game, playing audio song,loading website in a browser etc.. it can be simultaneous by the processor.
Thread based multithreading:
Whenever same part of program or multiple parts of programs are executing simultaneously known as thread based multi tasking.
Whenever multiple threads are running simultaneously is known as multithreading. Thread is a flow of execution, in real time it can be treated as request from the end user. threads are mainly classified into two types.
If any thread is running in the foreground of Python program is known as foreground thread. example: main method or main thread.
If any thread is running in the background of Python program is known as background thread example: garbage collector.
If any thread is created without allocation of memory space then the thread is comes under the new state. Ready State:
Whenever sufficient memory is allocated for the thread and if it is ready to execute then the thread under the ready state. Running State:
Whenever a thread is under execution then that can be treated as running state. Waiting state:
if any thread execution is stopped temporary is known as waiting state. Dead state:
If any thread execution is stopped permanently then that is comes under the dead state.
Whenever thread is in either new or dead state no memory is available and it remaining three cases (ready, running, waiting) sufficient memory is allocated. In realtime multithreading can be used either to design server softwares or different gaming softwares etc….
Types of threads:
In Python language threads are classified into two types.
2.User defined thread Predefined thread:
If any thread is already designed by Python developers is known as predefined thread. Example:
main thread, garbage collector , etc…..
User defined thread:
If any thread is designed by the Programmer is known as user defined thread.
1.Create a user defined class and extends thread class
2.create a constructor and call the predefined thread class constructor
3.Override run method of super class into user defined thread class.
4.Create an object (thread) for the user defined class.
5.Call run method with the help of start method. Syntax:
Write a Python program to display message a 2sec time gap.
class myThread (threading.Thread):
counter =counter+ 1
thread1 = myThread()
# Start new Threads
Thread class methods:
run() − The run() method is the entry point for a thread.
start() − The start() method starts a thread by calling the run method.
join([time]) − The join() waits for threads to terminate.
isAlive() − The isAlive() method checks whether a thread is still executing.
getName() − The getName() method returns the name of a thread.
setName() − The setName() method sets the name of a thread.
The threading module provided with Python includes a simple-to-implement locking mechanism that allows you to synchronize threads. A new lock is created by calling the Lock() method, which returns the new lock.
The acquire(blocking) method of the new lock object is used to force threads to run synchronously. The optional blocking parameter enables you to control whether the thread waits to acquire the lock.
If blocking is set to 0, the thread returns immediately with a 0 value if the lock cannot be acquired and with a 1 if the lock was acquired. If blocking is set to 1, the thread blocks and wait for the lock to be released.
The release() method of the new lock object is used to release the lock when it is no longer required.