0% found this document useful (0 votes)
80 views

Experiment No. - 13: AIM - Write A Program To Implement Producer Consumer Problem in Java

The document describes implementing a producer-consumer problem in Java using threads. The producer's job is to generate data and put it in a fixed-size buffer, while the consumer removes data from the buffer. To prevent overflow or empty buffer issues, the producer sleeps if the buffer is full and the consumer sleeps if it is empty. When data is added or removed, the sleeping thread is notified to prevent deadlock. The code implements classes for the producer, consumer and shared buffer (Q class), and creates producer and consumer threads to run the program.

Uploaded by

Mamta Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Experiment No. - 13: AIM - Write A Program To Implement Producer Consumer Problem in Java

The document describes implementing a producer-consumer problem in Java using threads. The producer's job is to generate data and put it in a fixed-size buffer, while the consumer removes data from the buffer. To prevent overflow or empty buffer issues, the producer sleeps if the buffer is full and the consumer sleeps if it is empty. When data is added or removed, the sleeping thread is notified to prevent deadlock. The code implements classes for the producer, consumer and shared buffer (Q class), and creates producer and consumer threads to run the program.

Uploaded by

Mamta Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT NO.

– 13
AIM – Write a Program to Implement Producer Consumer problem in Java
using Thread.

Producer-Consumer solution using threads in Java

In computing, the producer-consumer problem (also known as the bounded-


buffer problem) is a classic example of a multi-process synchronization
problem. The problem describes two processes, the producer and the consumer,
which share a common, fixed-size buffer used as a queue.

· The producer’s job is to generate data, put it into the buffer, and start again.

· At the same time, the consumer is consuming the data (i.e. removing it from
the buffer), one piece at a time.

Problem:

To make sure that the producer won’t try to add data into the buffer if it’s full
and that the consumer won’t try to remove data from an empty buffer.

Solution:

The producer is to either go to sleep or discard data if the buffer is full. The next
time the consumer removes an item from the buffer, it notifies the producer,
who starts to fill the buffer again. In the same way, the consumer can go to sleep
if it finds the buffer to be empty. The next time the producer puts data into the
buffer, it wakes up the sleeping consumer.

An inadequate solution could result in a deadlock where both processes are


wait- ing to be awakened.
CODE –

import java.util.Scanner;

class Q {

int n;

boolean signal = false;

synchronized int consume() {

while(!signal)

try {

System.out.println("Consumer thread sleeps");

wait();

} catch(InterruptedException e)

{ System.out.println("InterruptedException

caught");

System.out.println("Consumer thread awakes");

System.out.println("Consumed: " + n);

signal = false;

notify();

return n;

synchronized void produce(int n) {

while(signal)

try {

System.out.println("Producer thread sleeps");


wait();

} catch(InterruptedException e)

{ System.out.println("InterruptedException

caught");

this.n = n;

System.out.println("Producer thread awakes");

signal = true;

System.out.println("Produced: " + n);

notify();

class Producer implements Runnable {

Q q;

int n;

Producer(Q q,int n) {

this.n=n;

this.q = q;

new Thread(this, "Producer").start();

public void run()

{ System.out.println("Producer thread

created"); int i = 0;

while(i<n)

{ q.produce(+

+i);
}

System.out.println("Producer thread sleeps");

class Consumer implements Runnable {

Q q;

int n;

Consumer(Q q,int n) {

this.q = q;

this.n=n;

new Thread(this, "Consumer").start();

public void run()

{ System.out.println("Consumer thread

created"); while(true) {

q.consume();

public class synch {

public static void main(String[] args) {

int n;

System.out.println("Enter the production limit");

Scanner sc=new Scanner(System.in);


n=sc.nextInt();

System.out.println("The production limit is "+n);

Q q = new Q();

new Producer(q,n);

new Consumer(q,n);

}
OUTPUT –

You might also like