Hey guys, in this article you will learn about a Python program that uses two threads, and using these two threads the program will try to print the alternative numbers.
There are multiple ways to synchronize two threads, first is using queue, next is Semaphore, Third is using the locking mechanism, fourth is using the conditional mechanism that is built using the locking mechanism.
Let's try to use conditional locking in order to solve this Python program.
Write a Python Program to Print Alternate Numbers Using 2 Threads. Implement Using Wait And Notify Construct
First let's import all the required functions from the threading module.
Define the first thread1() function that takes Global variables such as number, target value and conditional variable. Now in this function you have to acquire the lock and check if the number is less than the target value it's so then print the number, increment the number and notify the other thread.
from threading import *
def thread1():
global number, target, cv
cv.acquire()
while number < target:
print("Thread 1 :",number)
number += 1
cv.notify()
cv.wait()
cv.notify()
cv.release()
Define the second thread function that again takes Global variables such as Number target value and conditional variable. now in this function so the thread will acquire the lock and wait until lock gets acquired.
Use the while loop to check if the number is less than the target value if so print the number and at the same time increment the number, notify the other thread that is waiting and execute wait().
def thread2():
global number, target, cv
cv.acquire()
cv.wait()
while number < target:
print("Thread 2 :",number)
number += 1
cv.notify()
cv.wait()
cv.notify()
cv.release()
Once the while loop is completed in both the Threads notify and release the lock()
In the main program take a conditional variable cv and initialize it by calling the condition() function. Take to Threads T1 and T2 and initialize the thread to the above provided functions.
cv = Condition()
T1 = Thread(target=thread2)
T2 = Thread(target=thread1)
Ask the user to enter the starting and ending numbers and store them into two variables, start the two threads using start function call and use the join() function to wait until the two Threads have exited.
number = int(input("Enter the starting number :"))
target = int(input("Enter the End number :"))
T1.start()
T2.start()
T1.join()
T2.join()
Complete Python Program
========================
from threading import *
def thread2():
global number, target, cv
cv.acquire()
cv.wait()
while number < target:
print("Thread 2 :",number)
number += 1
cv.notify()
cv.wait()
cv.notify()
cv.release()
def thread1():
global number, target, cv
cv.acquire()
while number < target:
print("Thread 1 :",number)
number += 1
cv.notify()
cv.wait()
cv.notify()
cv.release()
cv = Condition()
T1 = Thread(target=thread2)
T2 = Thread(target=thread1)
number = int(input("Enter the starting number :"))
target = int(input("Enter the End number :"))
T1.start()
T2.start()
T1.join()
T2.join()
Output
==================
Enter the starting number :6
Enter the End number :15
Thread 1 : 6
Thread 2 : 7
Thread 1 : 8
Thread 2 : 9
Thread 1 : 10
Thread 2 : 11
Thread 1 : 12
Thread 2 : 13
Thread 1 : 14
Output 2
==================
Enter the starting number :-4
Enter the End number :19
Thread 1 : -4
Thread 2 : -3
Thread 1 : -2
Thread 2 : -1
Thread 1 : 0
Thread 2 : 1
Thread 1 : 2
Thread 2 : 3
Thread 1 : 4
Thread 2 : 5
Thread 1 : 6
Thread 2 : 7
Thread 1 : 8
Thread 2 : 9
Thread 1 : 10
Thread 2 : 11
Thread 1 : 12
Thread 2 : 13
Thread 1 : 14
Thread 2 : 15
Thread 1 : 16
Thread 2 : 17
Thread 1 : 18
Output 3
=================
Enter the starting number :9
Enter the End number :4
Process finished with exit code 0
Conclusion
===============
The above Python program uses a conditional variable that internally locks the Global variables and we write operations. It is performed by continuously calling wait and notify functions. The wait() function acquires the lock and notify() function notifies the other thread to acquire the lock.
The program continuously prints the alternative numbers from starting range to the ending range until the target is completed. The program users while loop to iterate from number to the target.
You can also modify the while loop to contain the inclusive values by slight change as shown below
while number <= target:
Replace the < than comparison with <= symbol and note down the result.
Comment it down below if you have any queries regarding the above Python program.
0 Comments