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

Thread Communication. Sample Input and Sample Output:: S.No: 25 Date: 2022-06-15

The document describes a Java program that implements the producer-consumer problem using inter-thread communication. The program defines a Producer class and Consumer class that extend the Thread class and use a shared Queue class to communicate. The Producer puts integers into the queue and Consumer gets integers from the queue in a synchronized manner using wait() and notify() methods. The main method creates instances of the Producer and Consumer classes and starts their threads to run concurrently for 5 iterations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Thread Communication. Sample Input and Sample Output:: S.No: 25 Date: 2022-06-15

The document describes a Java program that implements the producer-consumer problem using inter-thread communication. The program defines a Producer class and Consumer class that extend the Thread class and use a shared Queue class to communicate. The Producer puts integers into the queue and Consumer gets integers from the queue in a synchronized manner using wait() and notify() methods. The main method creates instances of the Producer and Consumer classes and starts their threads to run concurrently for 5 iterations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Exp.

Name: Program that correctly implements Producer Consumer problem using the
S.No: 25 Date: 2022-06-15

ID: 20095A0363
    Page No:
           
concept of Inter Thread communication.

Aim:
Write a Java program that correctly implements Producer Consumer problem using the concept of Inter
Thread communication.
Sample Input and Sample Output:

PUT:0

GET:0

PUT:1

GET:1

PUT:2

GET:2

PUT:3

GET:3

PUT:4

GET:4

PUT:5

GET:5

Note: Iterate the while-loop in run() method upto 5 times in Producer and Consumer Class.
Source Code:

ProdCons.java

    Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous)      2019-2023-MECH-FDH


class Q {

int n;

boolean ValueSet =false;

synchronized int get(){

while(!ValueSet)

try {

wait();

catch(InterruptedException e) {

System.out.println("InterruptedException caught");

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

ValueSet =false;

notify();
return n;
}

synchronized void put (int n) {

while (ValueSet)

try{

wait();

catch(InterruptedException e) {

System.out.println("InterruptedException Caught");

this.n =n;

ValueSet =true;

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

notify();
}

ID: 20095A0363
    Page No:
           
class Producer implements Runnable {

Q q;

int n;

boolean stop=false;

Producer(Q q,int n) {

this.q=q;
this.n =n;

public void run(){

for(int i=0;i<n;i++)

q.put(i);

class Consumer implements Runnable {

Q q;

int n;

boolean stop =false;

Consumer(Q q,int n) {

this.q=q;
this.n=n;
}

public void run(){

    Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous)      2019-2023-MECH-FDH


for(int i=0;i<n;i++){

q.get();

class ProdCons {

public static void main(String args[]) throws Exception{

Q q=new Q();

int n=6;

Producer p=new Producer(q,n);

Consumer c=new Consumer(q,n);

Thread t=new Thread(p);

Thread t1=new Thread(c);

t.start();

t1.start();

Execution Results - All test cases have succeeded!

Test Case - 1

User Output
PUT:0
GET:0
PUT:1
GET:1
GET:5
PUT:5
GET:4
PUT:4
GET:3
PUT:3
GET:2
PUT:2
Test Case - 1

ID: 20095A0363
    Page No:
           
    Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous)      2019-2023-MECH-FDH

You might also like