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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:00:24+00:00 2026-06-15T15:00:24+00:00

I am trying to find out whether it is possible to have a multiple

  • 0

I am trying to find out whether it is possible to have a multiple producer / multiple consumer queue where I can use notify() instead of notifyAll(). For example, in the implementation below (source: here) you cannot just simply switch the notifyAll() for notify(). It is not totally obvious why you cannot switch so I will leave it as an teaser to whoever wants to help me out understanding this problem.

So the code below is broken:

public class BlockingQueue {

  private Object lock = new Object();

  private List queue = new LinkedList();
  private int  limit = 10;

  public BlockingQueue(int limit){
    this.limit = limit;
  }


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


  public Object dequeue()
  throws InterruptedException{
   synchronized(lock) {
    while(this.queue.size() == 0){
      lock.wait();
    }
    if(this.queue.size() == this.limit){
      lock.notify();
    }

    return this.queue.remove(0);
  }
 }
}
  • 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-15T15:00:25+00:00Added an answer on June 15, 2026 at 3:00 pm

    The following steps lead us to deadlock. Let’s set limit to 1 to keep the example brief.

    • E1 enqueues an item.
    • E2 attempts enqueue – checks wait loop – already full – waits
    • E3 attempts enqueue – checks wait loop – already full – waits

    • D1 attempts dequeue – and is executing synchronized block

    • D2 attempts dequeue – blocks on entry to the (synchronized) block – due to D1
    • D3 attempts dequeue – blocks on entry to the (synchronized) block – due to D1

    • D1 is executing enqueue – gets the item, calls notify, exits method

    • The notify happens to wake up E2 (i.e. “any waiting thread”)
    • BUT, D2 enters sync block before E2 can (E2 must reacquire the lock), so E2 blocks on entry to the enqueue sync block
    • D2 checks wait loop, no more items in queue, so waits
    • D3 enters block after D2, but before E2, checks wait loop, no more items in queue, so waits

    • Now there is E3, D2, and D3 waiting!

    • Finally E2 acquires the lock, enqueues an item, calls notify, exits method

    • E2’s notification wakes E3 (remember any thread can be woken)

    • E3 checks the wait loop condition, there is already an item in the queue, so waits.
    • NO MORE THREADS TO CALL NOTIFY and THREE THREADS PERMANENTLY SUSPENDED!

    SOLUTION: Replace notify with notifyAll

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

Sidebar

Related Questions

I am trying to find out whether it is possible to start a gen_server
I am trying to find out whether local press releases may have affected companies'
Trying to find out if it's possible to use SQLAlchemy on Heroku. Thanks.
I'm trying to find out whether it is possible to engineer a SSRS report
I am trying to find out whether it is possible to check if a
I'm trying to find out whether it's possible to invoke node.js functions from a
I'm trying to find out if it's possible to detect whether a user is
I'm trying to find out whether a Socket is currently connected - according to
What I'm trying to find out is whether there is some sort of equivalence
I am trying to evaluate user-submitted urls to find out whether they contain valid

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.