Skip to content

Instantly share code, notes, and snippets.

@alghanmi
Created October 31, 2013 05:58
Show Gist options
  • Select an option

  • Save alghanmi/7244899 to your computer and use it in GitHub Desktop.

Select an option

Save alghanmi/7244899 to your computer and use it in GitHub Desktop.
Producer Consumer Shared Data Model for CSCI 201
/*
* Figure 2-27
* Main Class which uses the ProducerConsumerMonitor
*/
import ProducerConsumerMonitor;
import Item;
class ProducerConsumer2 extends Thread {
private ProducerConsumerMonitor theMonitor;
// Inner classes for the Producer and Consumer
class Producer extends Thread {
public void run() {
while(true) {
Item data = produce_item();
System.out.println("Producer trying to insert");
theMonitor.insert(data);
}
}
private Item produce_item(){
Item data;
try{sleep(1000);}
catch(InterruptedException ex){};
data = new Item(0,itemCount++);
System.out.println("Producer making item " + data);
return data;
}
// Count of Items created
private int itemCount = 0;
}
public class Consumer extends Thread {
public void run() {
while(true) {
System.out.println("Consumer trying to remove");
Item data = theMonitor.remove();
consume_item(data);
}
}
private void consume_item(Item data){
System.out.println("Consumer used item " + data);
try{sleep(7000);}
catch(InterruptedException ex){};
}
}
// What the ProducerConsumer Thread does
public void run(){
theMonitor = new ProducerConsumerMonitor();
Producer p;
Consumer c;
// Make and start the producer
p = new Producer();
p.start();
// Make and start the consumer
c = new Consumer();
c.start();
}
// Start the whole thing going
public static void main(String args[]) {
ProducerConsumer2 pc = new ProducerConsumer2();
pc.start();
}
}
/*
* Figure 2-27
* Main Class which uses the ProducerConsumerMonitor
* with multiple producers and consumers
*/
import ProducerConsumerMonitor;
import Item;
class ProducerConsumer3 extends Thread {
private ProducerConsumerMonitor theMonitor;
// Inner classes for the Producers and Consumers
class Producer extends Thread {
public void run() {
while(true) {
Item data = produce_item();
System.out.println("Producer " + myNumber
+ " trying to insert");
theMonitor.insert(data);
try{sleep(1000);}
catch(InterruptedException ex){};
}
}
private Item produce_item(){
Item data;
try{sleep(1000);}
catch(InterruptedException ex){};
data = new Item(myNumber,itemCount++);
System.out.println("Producer " + myNumber
+ " making item " + data);
return data;
}
// Count of Items created
private int itemCount;
// Identification number for the Producer
private int myNumber;
public Producer(int x){
myNumber = x;
itemCount = 0;
}
}
public class Consumer extends Thread {
public void run() {
while(true) {
System.out.println("Consumer " + myNumber
+ " trying to remove");
Item data = theMonitor.remove();
consume_item(data);
}
}
private void consume_item(Item data){
System.out.println("Consumer " + myNumber
+ " used item " + data);
try{sleep(1000);}
catch(InterruptedException ex){};
}
// Identification number for the Consumer
private int myNumber;
public Consumer(int x){
myNumber = x;
}
}
// What the ProducerConsumer Thread does
public void run(){
theMonitor = new ProducerConsumerMonitor();
final int PRODUCERS = 10;
final int CONSUMERS = 10;
Producer p;
Consumer c;
// Make and start the Producer threads
for(int i=0; i<PRODUCERS; i++){
p = new Producer(i);
p.start();
}
// Make and start the Consumer threads
for(int i=0; i<CONSUMERS; i++){
c = new Consumer(i);
c.start();
}
}
// Start the whole thing going
public static void main(String args[]) {
ProducerConsumer3 pc = new ProducerConsumer3();
pc.start();
}
}
/*
* Figure 2-27
*/
import java.util.Vector;
import Item;
class ProducerConsumerMonitor extends Object {
private final int N = 5;
private int count = 0;
private Vector theData;
synchronized public void insert(Item data) {
while (count == N) {
try{
System.out.println("\tFull, waiting");
wait(5000); // Full, wait to add
} catch (InterruptedException ex) {};
}
insert_item(data);
count++;
if(count == 1) {
System.out.println("\tNot Empty, notify");
notify(); // Not empty, notify a
// waiting consumer
}
}
synchronized public Item remove() {
Item data;
while(count == 0)
try{
System.out.println("\tEmpty, waiting");
wait(5000); // Empty, wait to consume
} catch (InterruptedException ex) {};
data = remove_item();
count--;
if(count == N-1){
System.out.println("\tNot full, notify");
notify(); // Not full, notify a
// waiting producer
}
return data;
}
private void insert_item(Item data){
theData.addElement(data);
}
private Item remove_item(){
Item data = (Item) theData.firstElement();
theData.removeElementAt(0);
return data;
}
public ProducerConsumerMonitor(){
theData = new Vector();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment