0% found this document useful (0 votes)
0 views3 pages

Python Exp4

Uploaded by

BADASS GAMING YT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

Python Exp4

Uploaded by

BADASS GAMING YT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment : 04

Title : Programs on Threading using python.

Theory:
A thread is a single sequential flow of execution within a program. In other words, it is a
lightweight process that allows a program to perform multiple tasks concurrently. By
using threads, a program can make more efficient use of CPU resources, reduce latency,
and improve responsiveness.

In Python, the threading module is used to create and manage threads. Some of the
commonly used functions in the threading module are:

● Thread: This function is used to create a new thread.


● start: This function is used to start a new thread.
● join: This function is used to wait for a thread to finish.
● enumerate: This function returns a list of all active threads in the
program. ● current_thread: This function returns the current thread
object.

The threading module also provides synchronization primitives such as Lock, Event,
Condition, and Semaphore to allow threads to communicate and synchronize with each
other.

Tthe concurrent.futures module provides a high-level interface for asynchronously


executing functions using threads or processes. It provides the ThreadPoolExecutor and
ProcessPoolExecutor classes to manage a pool of threads or processes that can execute
tasks concurrently.

Program:
from threading import*
class myThread(Thread):
def run(self):
print('Thread is running...')

t1=myThread()
t1.start()
Aim : To Use single thread multiple time

from threading import*


from time import sleep
class Numbers(Thread):
def run(self):
for i in range(1,11,1):
sleep(2)
print(i)
t1=Numbers()
t1.start()
t1.join()
t2=Numbers()
t2.start()

Aim : Multi threading

from threading import*


from time import sleep
class Square(Thread):
def calsq(self):
squ=1
for i in range(11):
squ=i**2
sleep(2)
print('squrare of',i,'Number is',squ)
def run(self):
self.calsq()

class calcube1(Thread):
def calcube(self):
cube = 1
for i in range(11):
cube = i ** 3
sleep(2)
print('Cube of', i, 'Number is', cube)

def run(self):
self.calcube()

t1=Square()
t1.start()
t1.join()
t2=calcube1()
t2.start()
Output:

Conclusion: We have learned programming on Threading using python

You might also like