Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7815003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:22:47+00:00 2026-06-02T05:22:47+00:00

So I’ve read quite about Thread Synchronization in Java. I’m currently trying the Bounded-Buffer

  • 0

So I’ve read quite about Thread Synchronization in Java. I’m currently trying the Bounded-Buffer problem. The producer will keep producing a product on the buffer while the consumer keeps consuming it.

The producer will wait if the buffer is full before producing another product.
The consumer will wait if the buffer is empty.

However, my problem is that the producer only starts producing when the buffer is empty until the it is full. The consumer only starts consuming when the buffer is full until it is empty.

Example (buffer size: 5)

Produced Product 1
Produced Product 2
Produced Product 3
Produced Product 4
Produced Product 5
Consumed Product 1
Consumed Product 2
Consumed Product 3
Consumed Product 4
Consumed Product 5
Produced Product 6
Produced Product 7
Produced Product 8
Produced Product 9
Produced Product 10
Consumed Product 6
Consumed Product 7
Consumed Product 8
Consumed Product 9
Consumed Product 10
Produced Product 11
Produced Product 12
Produced Product 13
Produced Product 14
Produced Product 15
Consumed Product 11
Consumed Product 12
Consumed Product 13
Consumed Product 14
Consumed Product 15
Produced Product 16
Produced Product 17
Produced Product 18
Produced Product 19
Produced Product 20
Consumed Product 16
Consumed Product 17
Consumed Product 18
Consumed Product 19
Consumed Product 20
Produced Product 21
Produced Product 22
Produced Product 23
Produced Product 24
Produced Product 25
Consumed Product 21
Consumed Product 22
Consumed Product 23
Consumed Product 24
Consumed Product 25
Produced Product 26
Produced Product 27
Produced Product 28
Produced Product 29
Produced Product 30
Consumed Product 26
Consumed Product 27
Consumed Product 28
Consumed Product 29
Consumed Product 30

I want it so that the producer produces as long as the buffer is not full, whether or not the buffer is empty. Subsequently, I want it so that the consumer consumes as long as the buffer is not empty, whether or not the buffer is full.

What’s wrong with my code?

import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import javax.swing.JOptionPane;

public class ProducerConsumer{

    final static Queue<Product> buffer = new LinkedList<>();
    private static int buffer_size, no_items, itemno = 1;
    private static Random r = new Random();
    public static void main(String[] args) {
        try{
            buffer_size = Integer.parseInt(JOptionPane.showInputDialog("Input Buffer size (default: 5)"));
            if(buffer_size<0){
                buffer_size = 5;
            }
        }catch(NumberFormatException nfe){
            buffer_size = 5;
        }
        try{
            no_items = Integer.parseInt(JOptionPane.showInputDialog("Input No. of Items (default: 10)"));
            if(no_items<0){
                no_items = 10;
            }
        }catch(NumberFormatException nfe){
            no_items = 10;
        }
        Producer producer = new Producer();
        Consumer consumer = new Consumer();
        producer.start();
        consumer.start();
    }

    static class Product {
        private String name = "Product X";
        private int productno;

        public Product(int productno) {
            this.productno = productno;
            this.name = "Product "+productno;
        }
        public int number() {
            return productno;
        }
        @Override
        public String toString() {
            return name;
        }
    }
    static class Producer extends Thread{
        public Producer(){
        }
        public void produce(){
            Product p = new Product(itemno++);
            try {
                this.sleep(r.nextInt(100));
            } catch (InterruptedException ex) {
                Thread.interrupted();
            }
            buffer.add(p);
            System.out.println("Produced "+p);
        }
        @Override
        public void run(){
            synchronized(buffer){
                while(itemno<=no_items){
                    while(buffer.size()==buffer_size){
                        try{
                            buffer.wait(100);
                        }catch(InterruptedException e){
                            Thread.interrupted();
                        }
                    }
                    produce();
                    buffer.notifyAll();
                }
            }
        }
    }
    static class Consumer extends Thread{
        public Consumer(){
        }
        public boolean consume(){
            try {
                this.sleep(r.nextInt(100));
            } catch (InterruptedException ex) {
                Thread.interrupted();
            }
            Product product = buffer.remove();
            System.out.println("Consumed "+product);
            return product.number()==no_items;
        }
        @Override
        public void run(){
            synchronized(buffer){
                boolean end = false;
                while(!end){
                    while(buffer.isEmpty()){
                        try{
                            buffer.wait(100);
                        }catch(InterruptedException e){
                            Thread.interrupted();
                        }
                    }
                    end = consume();
                    buffer.notifyAll();
                }
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:22:48+00:00Added an answer on June 2, 2026 at 5:22 am

    It’s because you have acquired the buffer monitor and produced N times before notifyAll and releasing the buffer monitor via exiting the synchronized block, all inside the while loop.

    Try putting while outside of the synchronized blocks, so you give the other thread a chance to acquire the lock during concurrent produces/consumes.

    Note that notifyAll does not actually effect other threads until the notifying thread has exited the synchronized region

    Edit: I ran it after making the suggested changes and I got the following output

    Produced Product 1
    Produced Product 2
    Produced Product 3
    Consumed Product 1
    Consumed Product 2
    Consumed Product 3
    Produced Product 4
    Produced Product 5
    Consumed Product 4
    Consumed Product 5
    Produced Product 6
    Consumed Product 6
    Produced Product 7
    Produced Product 8
    Produced Product 9
    Produced Product 10
    Consumed Product 7
    Consumed Product 8
    Consumed Product 9
    Consumed Product 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.