Consider a simulation with three smoker threads and one agent thread. Each smoker continuously makes a cigarette and smokes it. But to make a cigarette, a smoker needs three ingredients: tobacco, paper, and matches. One of the smoker threads has only paper, another has only tobacco, and the third has only matches. The agent thread has an infinite supply of all three materials. The three smoker threads are initially blocked. The agent places two randomly chosen (different) ingredients on the table and unblocks the one smoker who has the remaining ingredient. The agent then blocks. The unblocked smoker removes the two ingredients from the table, makes a cigarette, and smokes it for a random amount of time, unblocking the agent on completion of smoking the cigarette. The agent then puts out another random two of the three ingredients, and the cycle repeats.

Write a multi-class multithreaded Java program that uses a monitor to synchronize the agent thread and the three smoker threads.

Do not mechanically translate semaphore code into monitor code! The agent thread executes in an agent object created from an agent class.

Each smoker thread executes in a smoker object. All smoker objects are created from one smoker class whose constructor is used to specify the ingredient possessed by the smoker object.

A driver class with a main method constructs the objects and starts the threads.

Use a single monitor object instantiated from a class Control for synchronization. Each of the four threads invokes a synchronized monitor method for its synchronization.

No semaphores are allowed. No synchronized blocks are allowed, only synchronized methods. No busy waiting is allowed. No calls to nap inside a synchronized method are allowed (do not nap while holding the monitor object's lock, that is, while inside a synchronized method or while inside a method called by a synchronized method).

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.