Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please tell me how to solve below code in Java Concurrency, Threads Write a program to simulate the behavior of supplier and consumer threads that
Please tell me how to solve below code in Java
Concurrency, Threads Write a program to simulate the behavior of supplier and consumer threads that exchange data as described below. Create a set of 10 supplier threads that create random data and pass the data to a set of 10 consumer threads. Each supplier and consumer thread has a unique id between 1 and 10. The supplier thread will supply the data to the consumer thread with the same matching id. The supplier and the consumer threads will meet in a synchronized common meeting room. At any time, there can be at most one supplier or consumer thread in the meeting room. Both consumer thread and supplier threads need to get access to the lock before they go in; the meeting room and leave the lock when they exit. Threads will block while competing for the lock. There can be at most one data field in the meeting room and that data will be marked for a specific consumer thread. Consume data (Sleep) Consumer Consumer / a. Wait for data from Su pplier (W3 it} Their interaction is illustrated in the above figure. Each supplier will sleep for a random time up to 1000 milliseconds, wakeup, create random number, go to the meeting room, and leave the random number for its consumer thread. The consumer thread will fetch the data and sleep for a random time up to 1000 milliseconds and then go back to fetch more data. If the supplier wants to leave data, but if there is unconsumed data in the meeting room, then it will go to waiting room until it is notified. When the consumer thread goes into the meeting room and finds the data is marked for its own consumption, it will copy the data and clear the fields in the meeting room so that the next supplier can leave its data. If the data is not marked for its use, then it will go to the waiting room and wait until it is notified. Insert diagnostic code in the supplier and consumer thread that identifies each step. For example, the supplier thread could print the following messages at each step: \"Supplier U sleeps 850 milliseconds. Supplier l sleeps 650 milliseconds. Supplier 0 produces data. Consumer D sleeps 950 milliseconds. Supplier O enters meeting room/leaves data for consumer 0. Supplier 0 leaves meeting room. Consumer l sleeps 250 milliseconds. Consumer l enters meeting room. Consumer l enters waiting room. Consumer 0 enters meeting room/ removes data. Consumer O sleeps for 500 millisecods. You could also enter the times in milliseconds at each of the above stages of the threadStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started