Question: The goal of this assignment is to write a multithreaded program that explores synchronization challenge. For this part, assume that, we have a shared variable

 The goal of this assignment is to write a multithreaded program

The goal of this assignment is to write a multithreaded program that explores synchronization challenge. For this part, assume that, we have a shared variable NextID. This is initialized to 1 at the beginning. Now create 5 threads in your program and assign ID 1, 2, 3, 4, 5 to them respectively. You can pass the ID as a parameter when you create the threads. Each of the threads will try to access the variable "NextID". Whenever a thread acquires the variable, it checks whether the NextId is equal to its own Id or not. If it is not equal, it will output "Not My Turn!", then print its threadId, and then release the variable. If it is equal, the thread will print ""My turn!", then print its threadId, increase the NextId by 1, and then release the variable. However, after increasing NextID by 1, the thread will check if the value is 6 or not. If it is 6, it will reset it to 1 before releasing the variable. The program should execute until each thread prints "Mu Turn!" 20 times. Once a thread prints for 20 times, it terminates. Count the number of times each thread prints "Not my Turn!" and include that in the report. The goal of this assignment is to write a multithreaded program that explores synchronization challenge. For this part, assume that, we have a shared variable NextID. This is initialized to 1 at the beginning. Now create 5 threads in your program and assign ID 1, 2, 3, 4, 5 to them respectively. You can pass the ID as a parameter when you create the threads. Each of the threads will try to access the variable "NextID". Whenever a thread acquires the variable, it checks whether the NextId is equal to its own Id or not. If it is not equal, it will output "Not My Turn!", then print its threadId, and then release the variable. If it is equal, the thread will print ""My turn!", then print its threadId, increase the NextId by 1, and then release the variable. However, after increasing NextID by 1, the thread will check if the value is 6 or not. If it is 6, it will reset it to 1 before releasing the variable. The program should execute until each thread prints "Mu Turn!" 20 times. Once a thread prints for 20 times, it terminates. Count the number of times each thread prints "Not my Turn!" and include that in the report

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!