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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:42:38+00:00 2026-05-31T17:42:38+00:00

I recently saw the following implementation of enqueue for a BlockingQueue ( source )

  • 0

I recently saw the following implementation of enqueue for a BlockingQueue
(source)

public synchronized void enqueue(Object item)
throws InterruptedException  {
  while(this.queue.size() == this.limit) {
    wait();
  }
  if(this.queue.size() == 0) {
    notifyAll();
  }
  this.queue.add(item);
}

Why is the while loop necessary, and can the while be replaced by if (this.queue.size() == this.limit)

It seems the method enqueue is synchronized so only 1 thread at a time can be executing in the method body and make the call to wait(). Once the thread is notified, can’t it just continue onward without checking the this.queue.size() == this.limit condition again?

  • 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-31T17:42:39+00:00Added an answer on May 31, 2026 at 5:42 pm

    No. You need the while because there might be multiple threads waiting for the space in the queue to open up, and the notifyAll() calls will wake all of them up.

    The wait() method actually releases the synchronisation monitor so that other threads can make progress. If it didn’t, then any thread trying to remove stuff from the queue would also get stuck waiting to enter a synchronised block in the get() method (for example).

    Only one of the waiting threads will see a partially empty queue.

    Actually, none of them might; the threads might have been woken up with notifyAll for some completely unrelated reason.

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

Sidebar

Related Questions

Recently I saw the following code that creates a class in javascript: var Model.Foo
I recently saw an interview question asking the following: Given a 32 bit number,
Possible Duplicate: Weird Java Boxing Recently I saw a presentation where was the following
I recently saw that the boost program_options library throws a logic_error if the command-line
I saw recently following code: #define MY_ASSERT_CONCAT_(a, b) a##b #define MY_ASSERT_CONCAT(a, b) MY_ASSERT_CONCAT_(a, b)
I've seen the wildcard used before to mean any object - but recently saw
I recently saw a C# constructor that look something like this... public Class foo
I recently saw this code being used in a source file in a C++
I recently saw this constructor in a class: public MyClass(){ } There were no
I recently saw a declaration of enum that looks like this: <Serializable()> <Flags()> Public

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.