Omaha Nebrasca
Omaha Nebrasca

Reputation: 67

How to implement an Variable Change Listener, for a variable running in different thread (example)

I have a AtomicInteger Variable (CurrentCycle) that is being updated in a separate thread to my UI thread. I am trying to update a text field in the UI field based on the the value of the AtomicInteger without an ugle while loop in the UI thread.

I've spent days searching online looking at related queries but am not getting anywhere on this. I'm looking for the simplest way to do this possible. I'm aware that there are posts out there for similar questions but I haven't been able to use them as they have been too abstract or for a different need.

My main questions are: 1. Do I need to register a listener to do this or is there a simpler way? 2. Where and how do I register the listener? Do I have to setup a separate interface class (I'd like to avoid this if possible). 3. Could you please some real example I could use - abstract explanations aren't helping me so far!

I've included my key code (which is not doing anything as the method is not being fired yet) below. Anyhelp you can provide would be greatly appreciated.


// MyActivity.java
    // Declaration (do I need this?)
        AtomicInteger CurrentCycle = new AtomicInteger(0);'


// Method which doesn't appear to be firing when CurrentCycle Changes...
    public void setVariableChangeListener(VariableChangeListener variableChangeListener) {
         this.variableChangeListener = variableChangeListener;

        if ((CurrentCycle != PreviousCycle ) && (this.variableChangeListener !=null)) {
            Toast.makeText(getApplicationContext(), "Test - code on variable change is firing.", Toast.LENGTH_SHORT).show();
           PreviousCycle=CurrentCycle;
    }

Upvotes: 2

Views: 3048

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Myself, I'd just give my class a PropertyChangeSupport field, give it an addPropertyChangeListener(...) and removePropertyChangeListener(...) methods, only allow the atomic variable to be changed in a setXxx(...) method, and in that same method, fire a property change event. I would take care that the fire event be called on the GUI's event thread. Note that if your GUI is a Swing GUI, then if you use a SwingPropertyChangeSupport, the threading issue is taken care of for you.

For example, from my answer to a similar question here:

public class ServerManager implements Runnable {
  public static final String IS_RUNNING = "is running"; // for the Event's name

  // if not a Swing app, then use a PropertyChangeSupport field
  private SwingPropertyChangeSupport propChngSupport = new SwingPropertyChangeSupport(this);
  private volatile boolean isRunning  = false;
  // other variables

  // addPropertyChangeListener(...) {...} goes here
  // removePropertyChangeListener(...)  {...} goes here

  public void setIsRunning(boolean isRunning) {
    boolean newValue = isRunning;
    boolean oldValue = this.isRunning;
    this.isRunning = isRunning;
    propChngSupport.firePropertyChange(IS_RUNNING, oldValue, newValue);    
  }

  public void run() {
    // ....
  }

  // other methods
}

As for your specific questions:

  • Do I need to register a listener to do this or is there a simpler way?

I don't see anything difficult about registering listeners.

  • Where and how do I register the listener?

The observer would register on the observed by calling some addListenerXxx(...) type method.

  • Do I have to setup a separate interface class (I'd like to avoid this if possible).

Not if you use PropertyChangeListener or Java's Observer/Observable class/interface combination

  • Could you please some real example I could use - abstract explanations aren't helping me so far!

You need to ask more specific questions than this one.

Upvotes: 1

Related Questions