How to detect if a variable has changed?

前端 未结 6 1361
生来不讨喜
生来不讨喜 2020-12-10 06:16

I have found myself wanting to do certain things in my programs only if a variable has changed. I have so far been doing something like this:

int x = 1;
in         


        
6条回答
  •  情歌与酒
    2020-12-10 06:42

    Assuming you have multiple threads you could create an object monitor and wait for the changed object to wake all blocked threads.

    Something like this:

    private MyObject myObject;
    
    ... other thread ...
    while(true) {
      myObject.wait()
      logic you want to run when notified.
    }
    ... other thread ...
    
    ... later on ...
    
    myObject.change();
    myObject.notifyAll();
    

提交回复
热议问题