Question
Application: The Consumer/Producer Problem 1. The Consumer/Producer problem was addressed in class and is described in module 5. A solution to this problem is presented
Application: The Consumer/Producer Problem
1. The Consumer/Producer problem was addressed in class and is described in module 5. A solution to this problem is presented on the picture below (in pseudo-language). This particular solution uses 3 semaphores: e, s, and n. Semaphore e is used to protect access to the critical sections. Semaphore e is used to ensure that the producer will not try and produce to a full buffer. Semaphore n is used to ensure that the consumer will not try and consume from an empty buffer.
Solve the Consumer/Producer problem using semaphores. A skeleton program is provided to you: The class Consumer.java represents the consumer, and the class Producer.java represents the producer. You need not modify Consumer.java or Producer.java, however, you should declare and initialize 3 semaphores in BoundedBuffer.java. Those semaphore should be used in the removeItem(..) and addItem(...) methods of BoundedBuffer.
import java.util.Random; public class Consumer extends Thread { private BoundedBuffer buffer; private final Random random = new Random(System.currentTimeMillis()); public Consumer(BoundedBuffer b) { buffer = b; } public void run() { int item; while (true) { try { //random number from 0 to 9.9999.... float sleepDuration = random.nextInt(10); Thread.sleep((int) (sleepDuration * 100)); } catch(InterruptedException e){ System.out.println(this.getName() + " got interrupted!"); } // consume an item from the buffer System.out.println("Consumer wants to consume."); item = buffer.removeItem(); } } } |
import java.util.Random; public class Producer extends Thread { private BoundedBuffer buffer; private final Random random = new Random(System.currentTimeMillis()); public Producer(BoundedBuffer b) { buffer = b; } public void run() { int item; while (true) { try { //random number from 0 to 9.9999.... float sleepDuration = random.nextInt(10); Thread.sleep((int) (sleepDuration * 10)); } catch(InterruptedException e){ System.out.println(this.getName() + " got interrupted!"); } // add an item into the buffer System.out.println("Producer wants to produce."); buffer.addItem(random.nextInt(10)); } } } |
public class BoundedBuffer { private static final int BUFFER_SIZE = 10; private int[] buffer; private int in, out; private int n; public BoundedBuffer(){ out = 0; //index of the item to be consumed next in = 0; //index of the item to be produced next n = 0; //number of items in the buffer buffer = new int [BUFFER_SIZE]; } public void addItem(int item){ //critical section below n++; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; System.out.println("Producer entered " + item + ". Buffer Size = " + n); //critical section above } public int removeItem(){ //critical section below n--; int item = buffer[out]; out = (out + 1) % BUFFER_SIZE; System.out.println("Consumer consumed " + item + ". Buffer Size = " + n); //critical section above return item; } public static void main(String[] args){ BoundedBuffer buffer = new BoundedBuffer(); Producer pThread = new Producer(buffer); Consumer cThread = new Consumer(buffer); pThread.start(); cThread.start(); } } |
Please provide your code (modified BoundedBuffer) and a sample output
Step 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