Bounded Buffer in - this.bbuffer = bbuffer This method is called when the thread runs public void run System.out.println"Consumer Thread

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Bounded Buffer in Java Implement a BoundedBuffer class to communicate producer and consumer threads using a cicular buffer. Similar to the BoundedBuffer class with Semaphores and Cndition Variables
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Bounded Buffer in Java BoundedBuffer.java: class BoundedBuffer { int [] buffer; int tail; int head; int n; int max; // Used to synchronize bounded buffer // as mutex and cond var. final Object monitor = new Object(); public BoundedBuffer(int max) { buffer = new int[max];
Background image of page 2
BoundedBuffer.enqueue(int v) public void enqueue(int v) { synchronized(monitor) { // Wait if buffer is full while (n==max) { try { monitor.wait(); } catch (InterruptedException e) {} } buffer[tail] = v; tail = (tail+1)%max; n++;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
BoundedBuffer.dequeue() public int dequeue() { int v; synchronized(monitor) { // Wait if buffer is empty while (n==0) { try { monitor.wait(); } catch (InterruptedException e) {} } v = buffer[head]; head = (head+1)%max; n--;
Background image of page 4
Bounded Buffer Consumer Thread Consumer.java: class Consumer extends Thread { BoundedBuffer bbuffer; Consumer(BoundedBuffer bbuffer) {
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: this.bbuffer = bbuffer; } // This method is called when the thread runs public void run() { System.out.println("Consumer Thread Running"); int last = -1; int i = 0; while (true) { i = bbuffer.dequeue(); Bounded Buffer Producer Thread Producer.java: class Producer extends Thread { BoundedBuffer bbuffer; public Producer() { bbuffer = new BoundedBuffer(10); } public void run() { System.out.println("Producer Thread Running"); int i = 0; // Start consumer Consumer consumer = new Consumer(bbuffer); consumer.start(); Bounded Buffer Producer Main public static void main(String [ ] args) { // Start producer. Producer will start consumer Thread thread = new Producer(); thread.start(); } } Bounded Buffer Output bash-4.0$ javac *.java bash-4.0$ java Producer bash-4.0$ java Producer Producer Thread Running Consumer Thread Running produce(0) produce(1) … produce(9) produce(10) consume(0) consume(1) consume(2) consume(3)...
View Full Document

This note was uploaded on 01/30/2012 for the course CS 354 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 8

Bounded Buffer in - this.bbuffer = bbuffer This method is called when the thread runs public void run System.out.println"Consumer Thread

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online