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 7872657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:16:12+00:00 2026-06-03T02:16:12+00:00

So I have simulated my producer consumer problem and I have the code below.

  • 0

So I have simulated my producer consumer problem and I have the code below. My question is this: how does the consumer stops if he’s in constant while(true).

In the code below, I’ve added

                    if (queue.peek()==null)
                         Thread.currentThread().interrupt();

which works nicely in this example. But in my real world design, this doesn’t work (sometimes it takes longer time to the producer to ‘put’ the data so the exception thrown in the consumer is incorrect. In general, I know I can put a ‘poison’ data such as Object is XYZ and I can check it in the consumer. But this poison makes the code really look bad. Wonder if anyone has a different approach.

public class ConsumerThread implements Runnable
{
 private BlockingQueue<Integer> queue;
 private String name;
 private boolean isFirstTimeConsuming = true;
 public ConsumerThread(String name, BlockingQueue<Integer> queue)
 {
    this.queue=queue;
    this.name=name;
 }

@Override
public void run()
{
    try
    {       
        while (true)
        {   
            if (isFirstTimeConsuming)
            {
                System.out.println(name+" is initilizing...");
                Thread.sleep(4000);
                isFirstTimeConsuming=false;
            }
            try{

                if (queue.peek()==null)
                    Thread.currentThread().interrupt();

                Integer data = queue.take();

                System.out.println(name+" consumed ------->"+data);
                Thread.sleep(70);    

            }catch(InterruptedException ie)
            {
                System.out.println("InterruptedException!!!!");
                break;
            }
        }

        System.out.println("Comsumer " + this.name + " finished its job; terminating.");

    }catch (InterruptedException e)
    {
        e.printStackTrace();
    } 
}

}

  • 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-03T02:16:14+00:00Added an answer on June 3, 2026 at 2:16 am

    A: There is simply no guarantee that just because peek returns null, the producer has stopped producing. What if the producer simply got slowed down? Now, the consumer quits, and the producer keeps producing. So the ‘peek’ -> ‘break’ idea basically fails.

    B: Setting a ‘done/run’ flag from consumer and reading it in producer also fails, if:

    1. consumer checks the flag, finds it should keep running, then does a ‘take’
    2. in meanwhile, producer was setting the flag to ‘dont run’
    3. Now consumer blocks forever waiting for a ghost packet

    The opposite can also happen, and one packet gets left out un-consumed.

    Then to get around this, you will want to do additional synchronization with mutexes over and above the ‘BlockingQueue’.

    C:
    I find ‘Rosetta Code’ to be fine source of deciding what is good practice, in situations like this:

    http://rosettacode.org/wiki/Synchronous_concurrency#Java

    The producer and consumer must agree upon an object (or an attribute in the object) that represents end of input. Then the producer sets that attribute in the last packet, and the consumer stops consuming it. i.e. what you referred to in your question as ‘poison’.

    In the Rosetta Code example above, this ‘object’ is simply an empty String called ‘EOF’:

    final String EOF = new String();
    
    // Producer
    while ((line = br.readLine()) != null)
      queue.put(line);
    br.close();
    // signal end of input
    queue.put(EOF);
    
    // Consumer
    while (true)
      {
        try
          {
            String line = queue.take();
            // Reference equality
            if (line == EOF)
              break;
            System.out.println(line);
            linesWrote++;
          }
        catch (InterruptedException ie)
          {
          }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that simulates mouse click. Code is something like this: [DllImport(user32.dll,
here's my problem: I have an application that launches a simulated server locally. The
I have a programming question, as follows, for which my solution does not produce
I have a simple simulated array with two elements: bowl["fruit"] = "apple"; bowl["nuts"] =
I have the following code: tmp_data = simulated_data[index_data]; unsigned char *dem_content_buff; dem_content_buff = new
I have to simulate family tree in prolog. And i have problem of symetrical
I have 4 files and the code 'works' as expected. I try to clean
I have images uploaded on the simulated sdcard on my emulator. I downloaded them
This is a problem I regularly bump into - hopefully someone can clarify to
I have a few checkboxes representing people. Something like this: [ ] George [

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.