Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.concurrent.Semaphore; class Buffer { private int data; / / Data variable to store the value produced / consumed private Semaphore mutex; / / Semaphore

import java.util.concurrent.Semaphore;
class Buffer {
private int data; // Data variable to store the value produced/consumed
private Semaphore mutex; // Semaphore for mutual exclusion
private Semaphore empty; // Semaphore to track empty slots in buffer
private Semaphore full; // Semaphore to track filled slots in buffer
// Constructor to initialize the semaphores
public Buffer(){
// Initialize semaphores
mutex = new Semaphore(1); // Binary semaphore for mutual exclusion
empty = new Semaphore(1); // Semaphore to track empty slots in buffer
full = new Semaphore(0); // Semaphore to track filled slots in buffer
}
// Method to produce data and add it to the buffer
public void produce(int newData) throws InterruptedException {
empty.acquire(); // Wait for an empty slot
mutex.acquire(); // Acquire mutex for critical section
// Add data to buffer
data = newData;
System.out.println("Produced: "+ newData);
mutex.release(); // Release mutex
full.release(); // Signal that buffer is full
}
// Method to consume data from the buffer
public int consume() throws InterruptedException {
full.acquire(); // Wait for a full slot
mutex.acquire(); // Acquire mutex for critical section
// Consume data from buffer
int consumedData = data;
System.out.println("Consumed: "+ consumedData);
mutex.release(); // Release mutex
empty.release(); // Signal that buffer is empty
return consumedData;
}
}
// Producer class implementing the Runnable interface
class Producer implements Runnable {
private Buffer buffer;
// Constructor to initialize the producer with a buffer
public Producer(Buffer buffer){
this.buffer = buffer;
}
// Override the run method to simulate production
@Override
public void run(){
try {
for (int i =0; i <10; i++){
buffer.produce(i); // Produce data
Thread.sleep(500); // Simulate production time
}
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
// Consumer class implementing the Runnable interface
class Consumer implements Runnable {
private Buffer buffer;
// Constructor to initialize the consumer with a buffer
public Consumer(Buffer buffer){
this.buffer = buffer;
}
// Override the run method to simulate consumption
@Override
public void run(){
try {
for (int i =0; i <10; i++){
buffer.consume(); // Consume data
Thread.sleep(1000); // Simulate consumption time
}
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
// Main class to execute the program
public class Main {
public static void main(String[] args){
Buffer buffer = new Buffer(); // Create a buffer instance
// Create producer and consumer threads
Thread producerThread = new Thread(new Producer(buffer));
Thread consumerThread = new Thread(new Consumer(buffer));
// Start producer and consumer threads
producerThread.start();
consumerThread.start();
}
} explain each line in this code - where is the race and how we deal with it why and how it's become deadlock happend

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

Recommended Textbook for

Accounting And Auditing Research And Databases Practitioner's Desk Reference

Authors: Thomas R. Weirich, Natalie Tatiana Churyk, Thomas C. Pearson

1st Edition

1118334426, 978-1118334423

More Books

Students also viewed these Databases questions

Question

What is cultural tourism and why is it growing?

Answered: 1 week ago