Answer the questions throughout the code: consumer.java: package producer_consum
ID: 3709442 • Letter: A
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 final Vector<Integer> sharedQueue;
private final int SIZE;
private final int NUM_PROCESSED;
public Consumer(Vector<Integer> 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<Integer> sharedQueue;
private final int SIZE;
private final int NUM_PROCESSED;
private boolean finished;
public Producer(Vector<Integer> 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<Integer> mean / do?
Vector<Integer> sharedQueue = new Vector<Integer>();
// 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?
}
}
Explanation / Answer
1. what does it mean that Consumer implements Runnable?
ans- It means that the class Consumer is creating a thread by implementing the Runnable interface.
4. what does wait() do? why are you doing it in this part of the program?
ans- Two threads can communicate with each other using wait() . This method is present in Object class and can be called
on any object, provided the thread should have the lock of that object and the thread should be inside synchronized
area.
We are using wait() here so that only one thread can access the synchronized block at a time and other threads
who are willing to access the synchronized block should wait for their turn.We have a synchronized block so that
only a producer or a consumer thread runs at a time.
5. what does notifyAll() do?
ans- notifyAll() also helps in interthread communication by waking up all threads that are waiting on this object's lock.
once you have something in list, you can have the consumer thread consume it, or if you have consumed something, you
can have the producer produce something.
6. what does remove(0) do?
ans- Removes the element from the stack memory at the specified position in the given Vector.
7. what does Vector<Integer> mean / do?
ans- Vector is a collection object which helps to creates a dynamic array of elements in the queue.
8. 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.
9. 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.
10. what does the join method of the thread object do?
ans- 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.
11. You get an error with the two joins.What is happening?What exactly is a InterruptedException?What happens if
either the producer thread or consumer thread stops executing unexpectedly? This code produces an infinity loop. Why?
ans- If a thread is interrupted during join() or sleep(), then it will throw an InterruptedException.
If either the producer thread or consumer thread stops executing unexpectedly then compiler will throw
this error.
In Producer class, we have an infinite outer loop to insert values in the list.In the Consumer class, we again have an infinite loop to extract a value from the list.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.