What is the source of deadlock in Producer Consumer problem with circular buffer

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

New contributor

Henry Lewis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật