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

  • SEARCH
  • Home
  • 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 6621759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:18:40+00:00 2026-05-25T21:18:40+00:00

I have to develop an application that is similar to the producer-consumer problem in

  • 0

I have to develop an application that is similar to the producer-consumer problem in java.

However I do not know a lot about java, and I have a couple of questions.

Both the producer and consumer that are different threads and they both need to access the same buffer. If they are both different classes (that either extends thread class or implement the runnable interface) how do I code them to use the exact same buffer (this supposed buffer is an array of a certain object)?

I also would like to read some suggestions about how the overall architecture and how should I implement them. I need to code them so that two threads don’t consume the same buffer position at the same time, that two producer threads don’t insert at the exact same value at the same time, producer can’t insert a new item in an already filled buffer, and that no consumer should consume when the buffer is empty.

In this example there must be several consumers and several producers working at the same time.

I looked for some examples in java but they are all different of what I need.

  • 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-05-25T21:18:40+00:00Added an answer on May 25, 2026 at 9:18 pm

    You can pass in a same instance of array or list to both consumer and producer by passing it through their constructor.

    Array a = new Array();
    Consumer c = new Consumer(a);
    Producer p = new Producer(a);
    

    For the second question, you would like to learn about (google it!) for synchronization in Java. You can again pass in the same private Object lock1 = new Object(); to both consumer and producer and they can use it as a shared lock.

    http://download.oracle.com/javase/tutorial/essential/concurrency/locksync.html

    Whenever a consumer or a producer access the shared array, they would need to acquire lock first. Other conditional requirements such as ‘not inserting elements when the array is full’ or ‘not consuming elements when the array is empty’ can be implemented inside the synchronized block.

    public void add(Object someObject){
        synchronized (lock1) {
            if(a.size()>limit) {
                System.out.println("Array is full");
            } else {
               a.add(someObject)
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to develop an application that display some information about the possessor of
I have to develop a Java ME (formerly known as J2ME) application that will
We have a .net application that we didn't develop, but use. I can tell
I have been trying to develop a peer to peer application that uses Micosoft's
I have been tasked to develop an application for my school that should be
I am looking to develop a Cocoa application that will only have a icon
I have to develop a small application that will connect to a Linux server
i'm need to develop and web application that uses maps, and i have two
We recently started using Eclipse to develop our java application and have been running
I have to develop a application that will communicate with some external device.I would

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.