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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:01:01+00:00 2026-06-05T11:01:01+00:00

The following is a simplified version of my current code. I am pretty sure

  • 0

The following is a simplified version of my current code. I am pretty sure I am not doing any thing wrong syntax-wise, and I can’t locate my conceptual mistake.

This is sort of an observer pattern I tried to implement. I could not afford to inherit from Java.utils.observable as my class is already complicated and inherits from another class.
There are two parts here:

There’s a Notifier class implementing Runnable :

public class Notifier implements Runnable{

    public void run()
    {
        while(true)
        {
            MyDataType data = getData();
            if(data.isChanged()==true)
            {
                refresh();
            }
        }
    }
}

And then there is my main class which needs to respond to changes to MyDataType data.

public class abc {

    private MyDataType data;

    public void abc(){
               Notifier notifier = new Notifier();
               Thread thread = new Thread(notifier);
               thread.start();
          }     


    public MyDataType getData(){
              return this.data;
    }


    public void refresh(){
         MyDatatype data = getData();
     //Do something with data
    }
}

The problem : What’s happening is that the notifier is calling refresh() when ‘data’ changes. However inside refresh(), when I do getData(), I am getting the old version of ‘data’!
I should mention that there are other parts of the code which are calling the refresh() function too.

  • What am I overlooking?
  • Any other better solutions to this problem?
  • How should I approach designing Subject-Observer systems if I can’t apply the default Java implementation out of the box?
  • 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-05T11:01:02+00:00Added an answer on June 5, 2026 at 11:01 am

    First, your data variable might be cached, so you will always need to get the latest value by making it volatile.

    Second, what you are doing here is a producer / consumer pattern. This pattern is usually best implemented with messages. When you receive new data, you could create an immutable object and post it to the consumer thread (via a thread safe queue like a BlockingQueue) instead of having a shared variable.

    Something along these lines:

    public class Notifier extends Thread{
       private BlockingQueue<E> consumerQueue = null;
       public setConsumerQueue(BlockingQueue<E> val){
          consumerQueue = val;
       }
       // main method where data is received from socket...
       public void run(){
          while(!interrupted()){
               data = ... // got new data here
               if(!data.isChanged()) continue;
               // Post new data only when it has changed
               if(consumerQueue!=null) consumerQueue.offer(data);
          }
       }
    }
    
    public class Consumer extends Thread{
       private BlockingQueue<E> consumerQueue = new BlockingQueue<E>();
       public Consumer (Producer val){
          val.setConsumerQueue(consumerQueue);
       }
       public void run(){
          while(!interrupted()){
               data = consumerQueue.take();// block until there is data from producer
               if(data !=null) processData(data);
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code (this is a simplified version of what I need) does not
The following code is a simplified version of what I use for event dispatching.
Please check the following section of code (Simplified version) my concern is in the
I have a code a following (simplified version): #define MESSAGE_SIZE_MAX 1024 #defined MESSAGE_COUNT_MAX 20
The following (simplified version of our) code passes our JUnit tests under Hibernate, but
Consider the following simplified version of my code. I have a template class A
The following code is the simplified version of my problem: public partial class Form1
The following code snippet is a simplified version of my issue. Basically I'm trying
Please see the following example which is a very simplified version of my code:
I have the following C++ code (simplified version): class Shape { bool isCircle =

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.