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

Multi Threading

Uploaded by

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

Multi Threading

Uploaded by

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

Thread1.

py

import threading

import time

def task1():

for i in range(5):

print('Rama-',i+1)

time.sleep(0.5)

def task2():

for i in range(5):

print('Sita-',i+1)

time.sleep(0.5)

#Creation of Thread object

th1=threading.Thread(target=task1)

th2=threading.Thread(target=task2)

t1=time.time()

th1.start()

th2.start()

th1.join()

th2.join()

t2=time.time()

print('Time Taken: ',t2-t1)


Thread2.py:

import threading

import time

class MyThread1(threading.Thread):

def run(self):

for i in range(5):

print('Rama-',i+1)

time.sleep(0.5)

class MyThread2(threading.Thread):

def run(self):

for i in range(5):

print('Sita-',i+1)

time.sleep(0.5)

t1=MyThread1()

t2=MyThread2()

t1.start()

t2.start()

import threading

import time
class Person1(threading.Thread):

def run(self):

global lock

lock.acquire()

print('Person-1 using the meeting room')

time.sleep(1)

print('Person-1 vacating the meeting room')

lock.release()

class Person2(threading.Thread):

def run(self):

global lock

lock.acquire()

print('Person-2 using the meeting room')

time.sleep(1)

print('Person-2 vacating the meeting room')

lock.release()

lock=threading.Lock()

t1=Person1()

t2=Person2()

t1.start()

t2.start()

You might also like