Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

answer the questions throughout the program package producer_consumer; import java.util.Vector; //what does it mean that Consumer implements Runnable? // ans:It means that the class Consumer

answer the questions throughout the program

package producer_consumer;

import java.util.Vector;

//what does it mean that Consumer implements Runnable?

// ans:It means that the class Consumer is creating a thread by implementing the Runnable interface.

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?

//2

this.sharedQueue = sharedQueue;

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

//6

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();

}

}

}

threadexample.java:

package producer_consumer;

import java.util.Vector;

public class ThreadExample {

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

// what does Vector mean / do?

//ans : Vector is a collection object which helps to creates a dynamic array of elements in the queue.

Vector sharedQueue = new Vector();

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

int vectorSize = 7;

int itemCount = 10;

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

// What is size? What is 10;

// ans: A thread is a path followed when executing a program. Every java program has atleast one thread

//called the main thread,invoked at the start of the program.

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

//--creates a thread named prodThread of class Producer which implements Runnable interface.

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

//--creates a thread named consThread of class Consumer which implements Runnable interface.

//The size is 4.

//The itemcount limit 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();

//ans- The start() is responsible for creating a separate call stack for the thread and then run() is called by JVM.

//On starting the thread, the overridden run() gets executed which defines the specific functionality of the thread.

//On commenting one of the either statements, we will not be able to start the thread.start() is very important to start

//the execution of the thread.

prodThread.start();

consThread.start();

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

//join() allows the current thread to wait until the thread on which it is called is dead.If thread is interrupted then it will throw InterruptedException.

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions