The cigarette smoker's problem is a classical synchronization problem that involves 4 threads: one agent and three smokers. The smokers require three ingredients in order to smoke a cigarette: tobacco, paper, and matches. Each smoker has one of the three ingredients and waits for the other two, smokes the cigar once he obtains all ingredients, and repeats this forever. The agent has an infinite supply of all three ingredients. The agent repeatedly places two of the ingredients on the table at a time. In the correct solution of this problem, the agent must signal the smoker with the complementary ingredient to pick up the two ingredients off the table and start making and smoking his cigar. The smoker must then signal the agent on completion of his cigar. The agent then puts out another two of the three ingredients, and the cycle repeats. Write a Java program to synchronize the agent and the smokers using semaphore.

You will be needing three semaphores initialized to 0 for the smokers, and one semaphore for the agent. Each smoker must first acquire a permission to pick up the two ingredients. The agent starts by randomly picking two of the three ingredients, places the two ingredients on the table, and then releases the semaphore of the smoker with the complementary ingredient. The agent should repeat this cycle for a predefined number of times.

Your solution must have two public classes called Agent and Smoker that extend Thread Also, you need to define a public class called Table that may contain any type of data structure such as ArrayList, Set, etc. to store the two ingredients. It should also contain two methods that implement the action of putting out and picking up the two ingredients.

At the end of your program, each smoker must print out the number of times that he smokes as shown in the sample output.

Output sample for part III:

Please note that your output results might be different. see image.

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.