Skip to content

Instantly share code, notes, and snippets.

@smadil997
Created September 25, 2021 14:58
Show Gist options
  • Save smadil997/ff79ccdb74a9ffb78e5c48e7f42df92c to your computer and use it in GitHub Desktop.
Save smadil997/ff79ccdb74a9ffb78e5c48e7f42df92c to your computer and use it in GitHub Desktop.
Consumer Producer Problem Solution In Multi Threading In Java
package com.practical.multi.thread;
import java.util.LinkedList;
public class ConsumerProducerProblemSolution {
public static void main(String ...args) {
//Class which contain consumer method and producer
ProducerAndConsumer producerAndConsumer=new ProducerAndConsumer();
Thread producer=new Thread(new Runnable() {
@Override
public void run() {
producerAndConsumer.producerCall();
}
});
Thread consumer=new Thread(new Runnable() {
@Override
public void run() {
producerAndConsumer.consumerCall();
}
});
// consumer and producer will start work
producer.start();
consumer.start();
try {
//here main Thread which main now will wait consumer and producer to complete their work
producer.join();
consumer.join();
} catch (InterruptedException ed) {
// TODO: handle exception
ed.printStackTrace();
}
}
}
class ProducerAndConsumer{
//Create buffer stack where producer and consumer will send and received data.
LinkedList<Integer> listStack=new LinkedList<Integer>();
//Defined Buffer size
int bufferSize = 2;
public void producerCall() {
int producedData =1;
try {
while(true) {
//Synchronized Block only one Thread can Enter
synchronized (this) {
// Producer will wait until buffer have not space to add more data and buffer size is only two as we already defined in #bufferSize
while(listStack.size() == bufferSize) {
wait();
}
//If buffer have empty or space to add more data
listStack.add(producedData++);
System.out.println("Producer Prodced Data as "+producedData);
Thread.sleep(3000); //Add Sleep for better understanding to log
//notify to consumer that buffer have data now so consumer can consume the data
notify(); //If you remove notify method so you will understand the consumer and producer problem
}
}
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void consumerCall() {
try {
while(true) {
//get locked on current thread.
//synchronized block for single thread can process consumer or producer only can process on buffer
synchronized (this) {
//Thread or Consumer will wait until producer not add data into buffer
while(listStack.size() == 0) {
wait();
}
// if buffer have data it will remove first element from top
// #removedData is only for reference to show in console
int removedData=listStack.removeFirst();
System.out.println("Consumer consumed the "+removedData);
Thread.sleep(3000); //Add Sleep for better understanding to log
// notify to producer thread that consumer consumed the data from buffer
notify(); //If you remove notify method so you will understand the consumer and producer problem
}
}
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment