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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:55:45+00:00 2026-06-11T17:55:45+00:00

Possible Duplicate: I am getting NullPointerException inside Consumer Is this considered to be a

  • 0

Possible Duplicate:
I am getting NullPointerException inside Consumer

Is this considered to be a bug in Producer , Consumer Scenario ??

This is my Producer class .

package com ;

import java.util.concurrent.PriorityBlockingQueue;

public class Producer extends CommonClass implements Runnable {
    private SyncronizedStack stack;
    private int producerNum;

    Producer(PriorityBlockingQueue<Character> queue) {
        this.queue = queue;
    }

    public void run() {

        char ch;

        for (ch = 'a'; ch <= 'f'; ch++) {
            queue.add(ch);
            System.out.println("Producer" + producerNum + "produced :" + ch);
            try {
                Thread.sleep((int) (Math.random() * 300));

            } catch (InterruptedException e) {
                System.out.println("Error");
            }

        }

    }

}

This is my Consumer class

package com ;

import java.util.concurrent.PriorityBlockingQueue;

public class Consumer extends CommonClass implements Runnable {

    private int consumerNum;

    Consumer(PriorityBlockingQueue<Character> queue) {
        this.queue = queue;
    }

    public void run() {
        char c;
        for (int i = 0; i < 7; i++) {
            try {
                c = queue.take();
                System.out.println("Consumer" + consumerNum + "consumed:" + c);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            try {
                Thread.sleep((int) (Math.random() * 300));
            } catch (InterruptedException e) {
                System.out.println("Error");
            }
        }

    }
}

This is my CommonClass

package com ;

import java.util.concurrent.PriorityBlockingQueue;

public class CommonClass {

    PriorityBlockingQueue<Character> queue = null;

}

This is my Client program

package com ;

import java.util.concurrent.PriorityBlockingQueue;

public class SyncTest {

    public static void main(String[] args) {

        CommonClass cs = new CommonClass();

        PriorityBlockingQueue<Character> queue = new PriorityBlockingQueue<Character>();

        Producer p1 = new Producer(queue);
        Thread t1 = new Thread(p1);
        t1.start();

        Consumer c1 = new Consumer(queue);
        Thread ct1 = new Thread(c1);

        ct1.start();

    }
}

When i run this i get output as

Producer0produced :a
Consumer0consumed:a

Producer0produced :b
Consumer0consumed:b

Producer0produced :c
Consumer0consumed:c

Producer0produced :d
Consumer0consumed:d

Producer0produced :e
Producer0produced :f
Consumer0consumed:e
Consumer0consumed:f

This is in case of e and f alphabets .

Is there any problem with this output ?? as i see the statement Producer0produced two time simultaneously and then followed by Consumer0consumed statement simultaneosuly for the letters e and f .

I fear that with this code will i miss any information produced by Producer ??

Please advice ??

Thanks in advice .

  • 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-11T17:55:47+00:00Added an answer on June 11, 2026 at 5:55 pm

    Is there any problem with this output ?

    Nope. Your producer thread just managed to produce a second item before the consumer managed to consume the first one. That’s entirely natural, particularly when you’re sleeping for random periods.

    Of course you may want to give an upper bound to your queue – and then use offer instead of add to detect the situation where the queue was full – but the output you’ve shown is fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Getting specific days in a month I've touched on this problem once
Possible Duplicate: Getting the class name from a static method in Java When you
Possible Duplicate: getting identity row value using ADO.NET Entity This is my example code:
Possible Duplicate: pthread Function from a Class I am getting an error (Can not
Possible Duplicate: Error: No previous prototype for function. Why am I getting this error?
Possible Duplicate: Why am I getting this error:“Cross-thread operation not valid: Control lbFolders accessed
Possible Duplicate: Getting unexpected output in program To be frank,this question may be silly
Possible Duplicate: Getting Segmentation Fault /* Reverse a string in place */ #import <stdio.h>
Possible Duplicate: Getting static property from a class with dynamic class name in PHP
Possible Duplicate: Declaring variables inside a switch statement I'm having difficulty getting XCode to

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.