# Class 1
def run(self):
for i in range(3):
print("FACE Prep")
# Class 2
def run(self):
for i in range(3):
print("Python")
# Main Method
t1 = FACE()
t2 = Python()
t1.run()
t2.run()
Output:FACE Prep
FACE Prep
FACE Prep
Python
Python
Python
Here, the main thread executes methods sequentially. To achieve parallel execution, we need to use threading.from threading import Thread
# Thread 1
class FACE(Thread):
def run(self):
for i in range(3):
print("FACE Prep")
# Thread 2
class Python(Thread):
def run(self):
for i in range(3):
print("Python")
# Main Method
t1 = FACE()
t2 = Python()
t1.start() # Start Thread 1
t2.start() # Start Thread 2
Output:FACE Prep
Python
FACE Prep
Python
FACE Prep
Python
sleep()
sleep()
method from the time
module.from time import sleep
from threading import Thread
class FACE(Thread):
def run(self):
for i in range(3):
print("FACE Prep")
sleep(1)
class Python(Thread):
def run(self):
for i in range(3):
print("Python")
sleep(1)
t1 = FACE()
t2 = Python()
t1.start()
t2.start()
Output:FACE Prep
Python
FACE Prep
Python
FACE Prep
Python
Here, threads alternate execution every second, demonstrating concurrent execution.join()
join()
to wait for thread completion.from time import sleep
from threading import Thread
class FACE(Thread):
def run(self):
for i in range(3):
print("FACE Prep")
sleep(1)
class Python(Thread):
def run(self):
for i in range(3):
print("Python")
sleep(1)
t1 = FACE()
t2 = Python()
t1.start()
t2.start()
t1.join() # Wait for Thread 1 to complete
t2.join() # Wait for Thread 2 to complete
print("Bye")
Output:FACE Prep
Python
FACE Prep
Python
FACE Prep
Python
Bye
Using join()
ensures that the main thread waits for other threads to finish.sleep()
method from the time
module.