I have a very stereotypical producer-consumer problem with a bounded buffer. It is running fine locally but when I submit to online I am getting an error that my Submission did not run 5 times successfully, indicating a deadlock. I am curious what could be the source for this? I feel like I did wait() and notify() correctly and I don’t know what other than that could be causing a deadlock.
I believe it has to be an error in Buffer.java, but I am including all the files in case.
Buffer.java:
package HW4;
public class Buffer {
private double[] array;
private int empty;
private int full;
private int capacity;
//Initialize values
public Buffer(int capacity) {
this.capacity = capacity;
this.array = new double[capacity];
this.empty = capacity;
this.full = 0;
}
//Produce method
//Check if full; add value to array; notify()
synchronized public void produce(double value) throws InterruptedException {
while (full == capacity) {
wait();
}
array[full] = value;
full++;
empty--;
notify();
}
//Consume method
//Check if empty; remove value from array; notify()
synchronized public double consume() throws InterruptedException {
while (empty == capacity) {
wait();
}
double retValue = array[capacity - empty - 1];
array[capacity - empty - 1] = 0d;
full--;
empty++;
notify();
return retValue;
}
}
Main.java:
package HW4;
public class Main {
public static final int iterations = 1000000;
public static void main(String[] args) {
//Create a buffer item along with a producer and consumer instantiation
Buffer buffer = new Buffer(1000);
Producer producer = new Producer(buffer, iterations);
Consumer consumer = new Consumer(buffer, iterations);
//Create a thread for both the producer and consumer; 1 of each
Thread prodThread = new Thread(producer);
Thread consThread = new Thread(consumer);
//Start each thread
prodThread.start();
consThread.start();
//Attempt to join the threads after they are done running.
try {
prodThread.join();
consThread.join();
} catch (InterruptedException e) {
System.out.println("ERROR IN MAIN.n");
}
System.out.println("Exiting!n");
}
}
Producer.java:
package HW4;
import java.util.Random;
public class Producer implements Runnable {
private Buffer buffer;
private Random random;
private int iterations;
//Import buffer item and create Random item for generating doubles
public Producer(Buffer buffer, int iterations) {
this.buffer = buffer;
this.random = new Random();
this.iterations = iterations;
}
//Loop to create 1,000,000 items and add them to the buffer
//Maintain total value for outputting
@Override
public void run() {
double total = 0d;
for (int i = 0; i < iterations; i++) {
double bufferElement = random.nextDouble() * 100.0;
try {
buffer.produce(bufferElement);
total += bufferElement;
} catch (InterruptedException e) {
System.out.println("ERROR in PRODUCER.n");
}
if (i % 100000 == 0 && i != 0) {
System.out.println("Producer: Generated " + i/1000 + ",000 items, Cumulative value of generated items=" + formatDouble(total));
}
}
System.out.println("Producer: Finished generating 1,000,000 items");
}
private String formatDouble(double value) {
return String.format("%.3f", value);
}
}
Consumer.java:
package HW4;
public class Consumer implements Runnable {
private Buffer buffer;
private int iterations;
//Import buffer item
public Consumer(Buffer buffer, int iterations) {
this.buffer = buffer;
this.iterations = iterations;
}
//Loop to remove 1,000,000 items from the buffer
//Maintain total value for outputting
@Override
public void run() {
double total = 0d;
for (int i = 0; i < iterations; i++) {
try {
double bufferElement = buffer.consume();
total += bufferElement;
} catch (InterruptedException e) {
System.out.println("ERROR in CONSUMER.n");
}
if (i % 100000 == 0 && i != 0) {
System.out.println("Consumer: Consumed " + i/1000 + ",000 items, Cumulative value of consumed items=" + formatDouble(total));
}
}
System.out.println("Consumer: Finished consuming 1,000,000 items");
}
private String formatDouble(double value) {
return String.format("%.3f", value);
}
}
Henry Lewis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.