Long description

Back

The 52 lines of code are as follows.

public class ProducerConsumer open curly bracket

static final int N equals to 100 semi colon forward slash forward slash constant giving the buffer size

static producer p equals to new producer open close parenthesis semi colon forward slash forward slash instantiate a new producer thread

static consumer c equals to new consumer open close parenthesis semi colon forward slash forward slash instantiate a new consumer thread

static our under score monitor mon equals to new our under score monitor open close parenthesis semi colon forward slash forward slash instantiate a new monitor

public static void main open parenthesis String argsopen close box bracket close parenthesis open curly bracket

p.start open close parenthesis semi colon forward slash forward slash start the producer thread

c.start open close parenthesis semi colon forward slash forward slash start the consumer thread

close curly bracket

static class producer extends Thread open curly bracket

public void run open close parenthesis open curly bracket forward slash forward slash run method contains the thread code

int item semi colon

while open parenthesis true close parenthesis open curly bracket forward slash forward slash producer loop

item equals to produce under score item open close parenthesis semi colon

mon.insert open parenthesis item close parenthesis semi colon

close curly bracket

close curly bracket

private int produce under score item open close parenthesis open curly bracket ... close curly bracket forward slash forward slash actually produce

close curly bracket

static class consumer extends Thread open curly bracket

public void run open close parenthesis open curly bracket run method contains the thread code

int item semi colon

while open parenthesis true close parenthesis open curly bracket

forward slash forward slash consumer loop

item equals to mon.remove open close parenthesis semi colon

consume under score item open parenthesis item close parenthesis semi colon

close curly bracket

close curly bracket

private void consume under score item open parenthesis int item close parenthesis open curly bracket ... close curly bracket forward slash forward slash actually consume

close curly bracket

static class our under score monitor open curly bracket forward slash forward slash this is a monitor

private int buffer open close box bracket equals to new intopen box bracket Nclose box bracket semi colon

private int count equals to 0 coma lo equals to 0 coma hi equals to 0 semi colon forward slash forward slash counters and indices

public synchronized void insert open parenthesis int val close parenthesis open curly bracket

if open parenthesis count equals to equals to N close parenthesis go under score to under score sleep open close parenthesis semi colon forward slash forward slash if the buffer is full coma go to sleep

buffer open box bracket hiclose box bracket equals to val semi colon forward slash forward slash insert an item into the buffer

hi equals to open parenthesis hi plus 1 close parenthesis percent N semi colon forward slash forward slash slot to place next item in

count equals to count plus 1 semi colon forward slash forward slash one more item in the buffer now

if open parenthesis count equals to equals to 1 close parenthesis notify open close parenthesis semi colon forward slash forward slash if consumer was sleeping coma wake it up

close curly bracket

public synchronized int remove open close parenthesis open curly bracket

int val semi colon

if open parenthesis count equals to equals to 0 close parenthesis go under score to under score sleep open close parenthesis semi colon forward slash forward slash if the buffer is empty coma go to sleep

val equals to buffer open box bracket loclose box bracket semi colon forward slash forward slash fetch an item from the buffer

lo equals to open parenthesis lo plus 1 close parenthesis percent N semi colon forward slash forward slash slot to fetch next item from

count equals to count minus 1 semi colon forward slash forward slash one few items in the buffer

if open parenthesis count equals to equals to N minus 1 close parenthesis notify open close parenthesis semi colon forward slash forward slash if producer was sleeping coma wake it up

return val semi colon

close curly bracket

private void go under score to under score sleep open close parenthesis open curly bracket try open curly bracket wait open close parenthesis semi colon close curly bracket catch open parenthesis Interrupted Exception exec close parenthesis open close curly bracket semi colon close curly bracket

close curly bracket

close curly bracket

Back