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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:17:26+00:00 2026-06-01T18:17:26+00:00

I am confused a bit by Java’s notify() . The following example is from

  • 0

I am confused a bit by Java’s notify(). The following example is from a textbook.

public synchronized consume() {
    while(queue.isEmpty()) {
        try{ wait(); } catch (InterruptedException e) {}
    }
    // Assume that getElement() notifies to producers.
    element = queue.getElement();
    ...
}

public synchronized produce() {
    while(queue.isFull()) {
        try{ wait(); } catch (InterruptedException e) {}
    }
    element = new Element();
    ...
    queue.addElement(element);
    notifyAll();
}

I quite understand the method produce() in the above example. However, can anyone tell me why we do not use notifyAll() at the end of the first method (consume())? For short, why not like this:

public synchronized consume() {
    while(queue.isEmpty()) {
        try{ wait(); } catch (InterruptedException e) {}
    }
    // Assume that getElement() notifies to producers.
    element = queue.getElement();
    ...
    notifyAll();
}

Thanks a lot!

Best regards.

  • 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-01T18:17:28+00:00Added an answer on June 1, 2026 at 6:17 pm

    I agree with you @Slash. In your code example, since you are removing and element from the queue then someone needs to notify the producers which are waiting in the isEmpty() loop. You code however says:

    // Assume that getElement() notifies to producers.
    element = queue.getElement();
    

    So that comment means that somehow getElement() calls notify() or notifyAll().

    For short, why not like this:

    Yes, your code would work although you need to remove the comment. 🙂

    Typically in these producer/consumer patterns, there are two locks: one when the queue is empty and one if it is full. Then the consumers can signal the producers when the queue is no longer full and not wake up the other consumers. In this case also, you could use a single notify() since you only need to awaken a single consumer or a single producer. The notifyAll() is used here because there is only one object being synchronized on.

    Also, for posterity, it is very important that you notice the while() loops are not if statements. Even when you are using notify() and two locks there are race conditions in a multiple producer/consumer model that have to be protected against. Also spurious wakeups are an issue. More on that here:

    http://256stuff.com/gray/docs/misc/producer_consumer_race_conditions/

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

Sidebar

Related Questions

I'm a bit confused from what I've heard Java doesn't do events. But I
While developing a web based java application I am a bit confused about the
I am bit confused about how Java handle conversion. I have char array consist
I'm a bit confused about how Java generics handle inheritance / polymorphism. Assume the
I'm currently learning the C programming language (coming from Java) and I'm a bit
The one class per file rule in Java has me a bit confused. I
I'm going through Java For Everyone by Cay Horstmann. I'm a bit confused on
I am bit confused so, thought to ask experts. I have written small java
I am a little bit confused about the use of Thread.yield() method in Java,
I am confused a bit about wait and notify/notifyAll. I know there is a

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.