Question: Answer the questions throughout the code: consumer.java: package producer_consumer; import java.util.Vector; //what does it mean that Consumer implements Runnable? class Consumer implements Runnable { private

Answer the questions throughout the code:

consumer.java:

package producer_consumer;

import java.util.Vector;

//what does it mean that Consumer implements Runnable?

class Consumer implements Runnable {

private final Vector sharedQueue;

private final int SIZE;

private final int NUM_PROCESSED;

public Consumer(Vector sharedQueue, int size, int numProcessed) {

// sharedQueue is passed as a parameter from ThreadExample.

// So are there two copies of the Vector or One?

this.sharedQueue = sharedQueue;

// how many copies of size are there among the three classes?

this.SIZE = size;

this.NUM_PROCESSED = numProcessed;

}

public void run() {

while (true) {

try {

System.out.println("Consumed: " + consume());

// this puts the thread to sleep (paused for a bit) change it here and in

// Producer to combinations of 0 and 200 .

// what changes about the program? Why?

Thread.sleep(150);

} catch (InterruptedException ex) {

System.out.println(ex);

ex.printStackTrace();

}

}

}

private int consume() throws InterruptedException {

//wait if queue is empty

while (sharedQueue.isEmpty()) {

synchronized (sharedQueue) {

System.out.println("Queue is empty " + Thread.currentThread().getName()

+ " is waiting , size: " + sharedQueue.size());

// what does wait() do? why are you doing it in this part of the program?

sharedQueue.wait();

}

}

//Otherwise consume element and notify waiting producer

synchronized (sharedQueue) {

// what does notifyAll() do?

sharedQueue.notifyAll();

// what does remove(0) do?

return (Integer) sharedQueue.remove(0);

}

}

}

producer. java:

package producer_consumer;

import java.util.Vector;

// what does it mean that Producer implements Runnable?

class Producer implements Runnable {

private final Vector sharedQueue;

private final int SIZE;

private final int NUM_PROCESSED;

private boolean finished;

public Producer(Vector sharedQueue, int size, int numProcessed) {

//sharedQueue is passed as a parameter from ThreadExample.

// So are there two copies of the Vector or One?

this.sharedQueue = sharedQueue;

// how many copies of size are there among the three classes?

this.SIZE = size;

this.NUM_PROCESSED = numProcessed;

}

public void run() {

for (int i = 0; i < NUM_PROCESSED; i++) {

System.out.println("Produced: " + i);

try {

produce(i);

// this puts the thread to sleep (paused for a bit) change it here and in

// Consumer to combinations of 0 and 200 .

// what changes about the program? Why?

Thread.sleep(150);

} catch (InterruptedException ex) {

System.out.println(ex);

ex.printStackTrace();

}

}

System.out.println("finished with producer");

}

private void produce(int i) throws InterruptedException {

//wait if queue is full

while (sharedQueue.size() == SIZE) {

synchronized (sharedQueue) {

System.out.println("Queue is full " + Thread.currentThread().getName()

+ " is waiting , size: " + sharedQueue.size());

// what does wait do? Why are you doing it in this part of the program?

sharedQueue.wait();

}

}

//producing element and notify consumers

synchronized (sharedQueue) {

sharedQueue.add(i);

// what does notifyAll() do?

sharedQueue.notifyAll();

}

}

}

thredexample.java:

package producer_consumer;

import java.util.Vector;

public class ThreadExample {

public static void main(String[] args) throws InterruptedException {

// what does Vector mean / do?

Vector sharedQueue = new Vector();

// how does size change the program? try 7. try 2.

int vectorSize = 4;

int itemCount = 10;

// what in the world is happening here?? What is a thread?

// What is size? What is 10;

Thread prodThread = new Thread(new Producer(sharedQueue, vectorSize,itemCount), "Producer");

Thread consThread = new Thread(new Consumer(sharedQueue, vectorSize,itemCount), "Consumer");

// what does the start method of the Thread object do?

// What runs when the start method is called?

// what happens if you comment out one of the following lines? Why?

prodThread.start();

consThread.start();

// what does the join method of the thread object do?

prodThread.join();

consThread.join();

// erase "throws InterruptedException". You get an error with the two joins.

// Fix it with a try catch. What is happening? What exactly is a InterruptedException?

// What happens if either the producer thread or consumer thread stops executing unexpectedly?

// HARD Question. This code produces an infinity loop. Why?

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!