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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:45:54+00:00 2026-06-13T12:45:54+00:00

I have the followings: public class ProducerConsumer { private BlockingQueue<Integer> q; private Random rnd;

  • 0

I have the followings:

public class ProducerConsumer {

  private BlockingQueue<Integer> q;
  private Random rnd;
  private boolean run;

  public ProducerConsumer(){
    rnd = new Random();
    q = new ArrayBlockingQueue<>(10);
    run = true;
  }

  // high leve - with ArrayBlockingQueue

  public void QProducer() throws InterruptedException{       
    int i;
    while(run){
        i  = rnd.nextInt(100);

        q.put(i);
        System.out.println(i +" Added. Size is: "+ q.size());
    }
  } 

  public void  QConsumer() throws InterruptedException{
    int i;
    while(run){
      Thread.sleep(100);
      if (rnd.nextInt(10) == 0) {

          i = q.take();
          System.out.println(i + " Taken. Size is: "+ q.size());
      }
    }
  }

public void changeRun(){
    run = false;
 }    
}

 public static void main(String[] args) throws InterruptedException {

    // Producer Consumer

    final ProducerConsumer pc = new ProducerConsumer();

    Thread t1 = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                pc.QProducer();
            } catch (InterruptedException ex) {
                Logger.getLogger(Threading.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });

    Thread t2 = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                pc.QConsumer();
            } catch (InterruptedException ex) {
                Logger.getLogger(Threading.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });

    t1.start();
    t2.start();

    Scanner scn = new Scanner(System.in);

    scn.nextLine();
    pc.changeRun();

    t1.join();
    t2.join();

}

Ouptput:

20 Added. Size is: 1
8 Added. Size is: 2
71 Added. Size is: 3
72 Added. Size is: 4
61 Added. Size is: 5
97 Added. Size is: 6
6 Added. Size is: 7
64 Added. Size is: 8
58 Added. Size is: 9
27 Added. Size is: 10
20 Taken. Size is: 10 *
93 Added. Size is: 10
8 Taken. Size is: 9
95 Added. Size is: 10
71 Taken. Size is: 10 *
70 Added. Size is: 10
72 Taken. Size is: 10 *
85 Added. Size is: 10
61 Taken. Size is: 9
43 Added. Size is: 10
64 Added. Size is: 10 **
... 

I wonder how can there be numbers that are taken, and yet the size remains the same ( * ),
and howcome values are added after Queue is full ( ** ).
AFAIU, BlockingQueue is sychronized, and waits for values to to be added if queue is empty, and to be removed if full.
Thank you in advance.

  • 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-13T12:45:55+00:00Added an answer on June 13, 2026 at 12:45 pm

    The put() and the println/size() after it, and the take() and the println/size() after it, can be interleaved because of the multithreading. So the value returned by size() may be out-of-date by the time it is printed. E.g.

    1a. Add (size = 10)
      1b. Print size 10
    2a. Take (size = 9)
    3a. Add (size = 10)
      2b. Print size 10
      3b. Print size 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following: public class Foo { private ReadingList mReadingList = new
Supposing I have the following public class Foo { private Map<Integer,SomeObject> myMap; public Foo()
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
lets say I have the following public class A { private string _someField; public
I have following class: public class PairOfDice { private Dice d1,d2; public int Value
So I have the following: public class Singleton { private Singleton(){} public static readonly
i have following class: public class FirstClass { public FirstClass() { list = new
I have following code I want to test: public class MessageService { private MessageDAO
I have following implementation public abstract class BaseAcion extends ActionSupport { private String result;
I have following service public class AppService : AsyncServiceBase<EvaluateStock> { public IBus Bus {

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.