Back to: Operating System
0
Practical 1(B)
Write a program to give a solution to the producer–consumer problem using message passing.
Algorithm:
Step 1: Start
Step 2: Initialize a producer with a fixed size message queue that is used to store the messages.
Step 3: The producer then produces a message and then places the in the message queue.
Step 4: If the message queue is full then the producer waits for the consumer.
Step 5: The consumer reads the message from the message queue.
Step 6: If the message queue is empty then the consumer has to wait for the producer.
Step 7: Stop
Code:
import java.util.Date;
import java.util.LinkedList;
import java.util.Queue;
class Producer extends Thread {
private int MAX_SIZE = 10;
private Queue<String> messageQueue = new LinkedList<String>();
@Override
public void run() {
try {
while (true) {
this.putMessage(new Date().toString());
sleep(((long) (Math.random() * 1000)));
}
} catch (InterruptedException ex) {
System.out.println("Error Ocurred" + ex.getLocalizedMessage());
}
}
public synchronized void putMessage(String message) {
try {
while (this.messageQueue.size() == MAX_SIZE) {
wait();
}
messageQueue.add(message);
notify();
} catch (InterruptedException ex) {
System.out.println("Error Ocurred" + ex.getLocalizedMessage());
}
}
public synchronized String getMessage() {
try {
while (this.messageQueue.isEmpty()) {
wait();
}
String message = this.messageQueue.remove();
System.out.println("Recieved : " + message);
notify();
return message;
} catch (InterruptedException ex) {
System.out.println("Error Ocurred" + ex.getLocalizedMessage());
}
return null;
}
}
class Consumer extends Thread {
private Producer producer;
Consumer(Producer producer) {
this.producer = producer;
}
@Override
public void run() {
try {
while (true) {
String message = producer.getMessage();
// sends a reply to producer got a message
System.out.println("Got message: " + message);
sleep(2000);
}
} catch (InterruptedException ex) {
System.out.println("Error Ocurred" + ex.getLocalizedMessage());
}
}
}
public class ProducerConsumerUsingMessagePassing {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Producer producer = new Producer();
producer.start();
new Consumer(producer).start();
}
} #This code is contributed by Subhashish Nabajja
Code:
import java.util.*;
public class ThreadProducer {
static List<Integer> list = new ArrayList<Integer>();
static class Producer implements Runnable {
List<Integer> list;
public Producer(List<Integer> list) {
this.list = list;
}
@Override
public void run() {
synchronized (list) {
for (int i = 0; i < 10; i++) {
if (list.size() >= 1) {
try {
System.out.println("producer is waiting ");
list.wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
System.out.println("produce=" + i);
list.add(i);
list.notifyAll();
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
}
static class Consumer implements Runnable {
List<Integer> list;
public Consumer(List<Integer> list) {
this.list = list;
}
@Override
public void run() {
synchronized (list) {
for (int i = 0; i < 10; i++) {
while (list.isEmpty()) {
System.out.println("Consumer is waiting");
try {
list.wait();
} catch (InterruptedException ex) {
ex.printStackTrace();;
}
}
int k = list.remove(0);
System.out.println("consume=" + k);
list.notifyAll();
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
Thread producer = new Thread(new Producer(list));
Thread consumer = new Thread(new Consumer(list));
producer.start();
consumer.start();
}
}# This code is contributed by Varaliya Mohammed