Producer Consumer Problem in Java

Sanjay Singh
2 min readSep 17, 2019

--

September 17, 2019

Producer — Consumer problem

Producer produce the jobs(add element into Queue) and Consumer consume the jobs(remove element from Queue) .
Both are shared a fixed size buffered memory as Queue.
We can solve Producer-Consumer problem
1- by using wait , notify() & notifyAll()
2- Semaphore

1- How to use wait, notify and notifyAll in Java — Producer Consumer Example

package com.application.producer;

import java.util.Queue;
import java.util.Random;

public class Producer extends Thread {

private final Queue<Integer> queue;
private final int maxSize;

public Producer(Queue<Integer> queue, int maxSize, String name) {
super(name);
this.queue = queue;
this.maxSize = maxSize;
}

@Override
public void run() {

while (true) {

synchronized (queue) {

while (queue.size() == maxSize) {
try {
System.out.println(“Buffred size is full”);
queue.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Random random = new Random();
int i = random.nextInt();
System.out.println(“Producing value : “ + i);

queue.add(i);
queue.notifyAll();

}
}
}

}

===========================

package com.application.producer;

import java.util.Queue;

public class Consumer extends Thread {

private final Queue<Integer> queue;

private final int maxSize;

public Consumer(Queue<Integer> queue, int maxSize, String name) {

super(name);

this.queue = queue;

this.maxSize = maxSize;

}

@Override

public void run() {

while (true) {

synchronized (queue) {

while (queue.isEmpty()) {

try {

queue.wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

queue.remove();

System.out.println(“Consuming the job”);

queue.notifyAll();

}

}

}

}

================

package com.application.producer;

import java.util.LinkedList;

import java.util.Queue;

import java.util.Random;

public class ProducerConsumerExample {

public static void main(String[] args) {

final Queue<Integer> buffer = new LinkedList<>();

int maxsize = 10;

Producer producer = new Producer(buffer, maxsize, “Producer”);

Consumer consumer = new Consumer(buffer, maxsize, “Consumer”);

Random random = new Random();

int i = random.nextInt();

System.out.println(“produce same” + i);

producer.start();

consumer.start();

}

}

========================================

Output

Producing value : 250629585

Producing value : -316327511

Producing value : -864679355

Producing value : 38374974

Producing value : 1229081603

Producing value : -775385974

Producing value : -822954575

Producing value : -78390043

Producing value : 44810827

Producing value : -1987790057

Buffred size is full

Consuming the job

Consuming the job

Consuming the job

Consuming the job

Consuming the job

Consuming the job

Consuming the job

Consuming the job

Consuming the job

Consuming the job

--

--

Sanjay Singh
Sanjay Singh

Written by Sanjay Singh

Java, Spring Boot & Microservices developer Sharing knowledge, tutorials & coding tips on my Medium page. Follow me for insights & see story list section

Responses (2)