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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:19:11+00:00 2026-05-31T06:19:11+00:00

I am trying to design a class as a Code Kata for myself that

  • 0

I am trying to design a class as a Code Kata for myself that has a value property that can be set, and the class can issue ValueListener instances. The idea is that there is one instance of ValueHolder with many client threads accessing it concurrently. Each client thread has requested a ValueWatcher and has called waitForValue().

What I am really struggling with is what condition I should use on the while loop around the wait() to avoid spurious notifications (i.e. value hasn’t changed). I can see that this design may make it possible of ValueWatcher instances to miss updates, but am less worried about that at this stage.

Would appreciate any guidance on offer!

public class ValueHolder {

  private int value = 0;
  private final Object monitor = new Object();

  public void setValue(int value) {
    synchronized(monitor) {
      this.value = value;
      monitor.notifyAll();
    }
  }

  ValueWatcher createChangeWatcher() {
    return new ValueWatcher();
  }

  private class ValueWatcher {
    public int waitForValue() {
      synchronized(monitor) {
        while (==== ??? =====) {
          monitor.wait();
          return value;
        }
      }
    }
  }     
}
  • 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-31T06:19:12+00:00Added an answer on May 31, 2026 at 6:19 am

    Interesting problem. Here’s one solution off the top of my head. Have a version number along with the value that is being changed. Whenever the value is updated, the version number is also incremented so the ValueWatcher objects can then check to see if the version went up meaning a change has happened.

    Edit:
    I initially had an AtomicLong but I am stealing the idea of a wrapper object from @John Vint.

    private final VersionValue versionValue = new VersionValue();
    
    public void setValue(int value) {
        synchronized (monitor) {
           versionValue.value = value;
           versionValue.version++;
           monitor.notifyAll();
        }
    }
    
     private class ValueWatcher {
         private long localVersion = 0;
         public int waitForValue() {
             synchronized (monitor) {
                 while (true) {
                     if (localVersion < versionValue.version) {
                         // NOTE: the value might have been set twice here
                         localVersion = versionValue.version;
                         return versionValue.value;
                     }
                     monitor.wait();
                 }
             }
         }
    }
    
    private static class VersionValue {
        int value;
        long version;
    }
    

    Also, although spurious wakeups are possible it is important to remember that the text:

    Always invoke wait inside a loop that tests for the condition being waited for. Don’t assume that the interrupt was for the particular condition you were waiting for, or that the condition is still true.

    Is more about race conditions and producer/consumer models than spurious wakeups. See my page here about that.

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

Sidebar

Related Questions

I'm trying to design a homepage for an MVC site that has two different
I am trying to work out the best way to design a class that
I'm trying to design model in Django. I want that App can link to
I am trying to design a helper class that implements methods using AsyncTask. public
I'm trying to design a policy-based class, where a certain interface is implemented by
I'm trying to generate a diagram for a design document. I've generated a class
I'm trying to design some tables to store some data, which has to be
I am trying to design a location lookup in which the user can specify
I am trying to understand the boost array . The code can be read
I was trying to incorporate the Singleton design pattern into my code, but I

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.