java - How to detect inside a method that another method in the same class has updated one of the classes variables? -


(newbie-warning)

i have class, myclass, implelements listener interface (somelistener). myclass contains 2 counters, counta , countb. myclass added listener otherclass. when happens in otherclass, fires myclass-methods incrementa or incremenetb.

myclass has method mymethod called thirdclass. sends list of requests server. server responds otherclass each of requests. otherclass fires incrementa or incrementb.

my problem: preferrably need mymethod return after server has responded otherclass requests. can't seem work. sketch of code far:

class myclass implements somelistener{      private int countera;     private int counterb;     private int numrequests;      public boolean mymethod(){          // send numrequests number of requests serve          long start = system.currenttimemillis();         long end = start + 1000*numobjects;          //wait responses requests, or timeout         while(countera+counterb < numrequests &&  currenttime<end){             //wait... in not elegant fashion         }          if(countera+counterb==numrequests){             //             return true         }          // timed out         //do else         return false;     }       public void incrementa(){         countera++;     }      public void incrementb(){         counterb++;     } }   class somelistener{     public void incrementa();     public void incrementb(); }   class otherclass{      myclass myclassobject = new myclassobject();      //receives response server , write log      if(response equals something)         myclassobject.incrementa;     else             myclassobject.incrementb; }   class thirdclass{      myclass myclassobject = new myclassobject();     if (myclassobject.mymethod()){         //     } } 

my problem mymethod doesn't register if countera or counterb incremented in incrementa/b-methods. runs timeout time, long after otherclass has received responses server. show me error of ways?

with scenario in concurrency land. move careful, easy hurt yourself.

one approach create countdownlatch , wait on in mymethod, count down latch in increment methods. note work when 1 thread @ atime might enter mymethod

see countdownlatch details how use countdownlatch


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -