Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Reference 2: Producer-Consumer Problem using Monitor in Java Solution to the Producer-Consumer Problem using Monitor in Java (synchronized feature in Java): This solution has four

Reference 2: Producer-Consumer Problem using Monitor in Java

Solution to the Producer-Consumer Problem using Monitor in Java (synchronized feature in Java):

This solution has four Java classes:

1) The outer class: ProducerConsumer

2) The 2nd and 3rd classes: Producer and Consumer

3) The monitor class: OurMonitor

REMARK: DO NOT COPY AND PASTE THE FOLLOWING CODE DIRECTLY (Hidden special characters may cause errors.).

import java.util.*;

public class ProducerConsumer { static final int N = 100; // constant for the buffer size

static Producer p = new Producer(); // instantiate a new Producer thread

static Consumer c = new Consumer(); // instantiate a new Consumer thread

static OurMonitor mon = new OurMonitor(); // instantiate a new OurMonitor

public static void main(String args[]) {

p.start(); // start the Producer thread c.start(); // start the Consumer thread } static class Producer extends Thread { public void run() { // run method with the thread code int item;

while (true) { // producer loop: infinite loop

item = produceItem();

mon.insert(item);

} // end of while } // run()

private int produceItem() {

Random rand = new Random();

int num = rand.nextInt(100)+1;

System.out.println("P:" + num + " ");

return num;

} // produceItem()

} // class Producer

static class Consumer extends Thread { public void run() { // run method with the thread code int item;

while (true) { // consumer loop : infinite loop

item = mon.remove();

consumeItem(item);

} // end of while } // run()

private void consumeItem(int item) {

System.out.println("C:" + item + " ");

} // consumeItem()

} // class Consumer

static class OurMonitor { // Monitor definition

// shared data of the Monitor

private int buffer[] = new int [N];

private int count = 0;

private int lo = 0, hi = 0;

// Monitor operations

// 1. insert() operator public synchronized void insert(int val) { if (count==N) gotoSleep(); // if the buffer is full, go to sleep

buffer[hi] = val; // insert an item into the buffer

hi = (hi+1) % N; // slot to place next item in

count = count + 1;

if (count==1) notify(); // if consumer was sleeping, wake it up

} // insert()

// 2. remove() operator public synchronized int remove() { int val;

if (count==0) gotoSleep(); // if the buffer is empty, go to sleep

val = buffer[lo]; // fetch an item from the buffer

lo = (lo+1) % N; // slot to fetch next item from

count = count - 1;

if (count==N-1) notify(); // if producer was sleeping, wake it up

return val;

} // remove()

private void gotoSleep() {

try {

wait(); // wait() can be interrupted

} catch(InterruptedException exc) {

System.out.println("Interrupt occurred!");

};

} // gotoSleep()

} // class OurMonitor

} // class ProducerConsumer

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions