Wednesday, August 9, 2023

Q6) Write a Python multithreading program to print the thread name and corresponding process for each task (assume that there are four tasks).

import threading

 

def task(task_number):

    print(f"Task {task_number} in thread {threading.current_thread().name}")

 

def main():

    tasks = 4

   

    threads = []

    for i in range(tasks):

        thread = threading.Thread(target=task, args=(i+1,))

        threads.append(thread)

        thread.start()

   

    for thread in threads:

        thread.join()

   

    print("All tasks are completed")

 

if __name__ == "__main__":

    main()

 

No comments:

Post a Comment