{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

COP4600_2-6

# COP4600_2-6 - COP4600*Exam#1 next wednesday An OO approach...

This preview shows pages 1–3. Sign up to view the full content.

COP4600 2-6-10 *Exam #1 next wednesday An OO approach to a P & C class MyBoundedBuffer: a first attempt class BrokenBoundedBuffer<E> implements BoundedBuffer { private E[] buffer; private int count; // number items in buffer private int in = 0; // where next item will be inserted private int out = 0; // where next item will be taken from public BrokenBoundedBuffer( int size ) { buffer = new E[ size ]; // Java will not allow this } public void insert( E item ) { while ( count == buffer.length ) ; //sit and spin until space avail: evil busy wait ++count; buffer [ in ] = item; in = ( in + 1 ) % buffer.length; } public E remove() { while ( count == 0 ) ; //sit and spin until an item becomes available --count; E item = buffer[ out ]; out = (out + 1) % buffer.length; return item; } } Scenario and the correct behavior of a BB in that context Assume: 1 P(roducer) and 1 C(onsumer) Assume: a P makes tuff 5X faster than a C can use it. P_ideal will show the behavior of a P with an unbounded buffer buffer will show the actual contents of a buffer of size 3 P_buff3 will show the behavior of a P with a |buffer| == 3

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

View Full Document
P_ideal Buffer3 P_buff3 C make() ___ make() remove() insert() X__ insert() make() X__ make() ___ consume() insert() ___ insert() make() _X_ make()
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}