开发者

Constantly check for change in Java?

开发者 https://www.devze.com 2022-12-28 22:28 出处:网络
How can I check for a boolean to change st开发者_如何学Cate over a given period of time, and if a change is made over that time period perform a method?

How can I check for a boolean to change st开发者_如何学Cate over a given period of time, and if a change is made over that time period perform a method?

Can any help please be given in Java.

Thanks.


Sounds like you want to wrap a boolean in a class which you can listen for changes on.

class ObservableBoolean {

    // "CopyOnWrite" to avoid concurrent modification exceptions in loop below.
    private final List<ChangeListener> listeners =
            new CopyOnWriteArrayList<ChangeListener>();

    private boolean value;

    public boolean getValue() {
        return value;
    }

    public synchronized void setValue(boolean b) {
        value = b;
        for (ChangeListener cl : listeners)
            cl.stateChanged(new ChangeEvent(this));
    }

    public synchronized void addChangeListener(ChangeListener cl) {
        listeners.add(cl);
    }

    public synchronized void removeChangeListener(ChangeListener cl) {
        listeners.remove(cl);
    }
}

Then simply do:

ObservableBoolean b = new ObservableBoolean();

//...

// Start the "period of time":
b.addChangeListener(iWantToBeNotifiedOfChanges);

// ...

// End the "period of time":
b.removeChangeListener(iWantToBeNotifiedOfChanges);

This is actually a simple case of the MVC pattern (and the observer pattern). The model in this case is the ObservableBoolean and the view would be the "view" that wants to be notified of the changes.

You could also write your own ChangeListener interface if you want to avoid a strange looking javax.swing... import


The easiest way to do this is with a wrapper class...

public class Bool
{
    public Bool(){ _val = false; }
    public Bool(boolean val) { _val = val; }

    public boolean getValue(){ return _val; }
    public void setValue(boolean val){ 
         _changesupport.firePropertyChange("value",_val,_val=val);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener ){
        _changesupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener){
        _changesupport.removePropertyChangeListener( listener );
    }

    private boolean _val = false;
    private final PropertyChangeSupport _changesupport = new PropertyChangeSupport(this);
}

This is a common pattern in Swing, and you can use PropertyChangeSupport to simplify the creation of objects on which you can observe and listen for property changes. With such classes, you can register a PropertyChangeListener to handle the resulting PropertyChangeEvents that result.


use Timer or write your own class extending Thread.

Googling those things should give you good start

edit : From question it is not quite clear what are his intends. Maybe he wants to invoke some method in exact period of time, not immediatelly after event. For this pattern, I'd still rather stick to timer, or write my own Thread. Writing own Thread is still best way of interpreting specific user requirements. On the other hand, if this is really case of listening to events, my Timer pattern would be completely wrong.

example : I want to update database (imagine web browser game) every 5 minutes, if users requests so (reques = true). With false request, I don't need to update database. Immediatelly update database (f.e. stats in game Tribal Wars with thoushands of players) is totally overkill.


Seems like you should look to implement some sort of Model View Controller Pattern. Have a look here

The basic idea is that you should fire an event when the boolean is changed, and then that event is fired, your listener should handle that.

0

精彩评论

暂无评论...
验证码 换一张
取 消