How can I make sure a method is only called once by multiple threads?

后端 未结 5 1584
孤城傲影
孤城傲影 2020-12-17 17:46

I have the following structure:

public void someMethod(){  
   //DO SOME STUFF
   try{  
    doSomeProcessing();  
   }  
   catch (Exception e){  
                


        
5条回答
  •  独厮守ぢ
    2020-12-17 18:34

    As I understand your question you need to load data in an unpredictable but limited time interval. There are three possible ways to do so: 1) You could surround your call to loadSomeHeavyData with an if statement which controls the access to the method. 2) You could change your method to handle the control flow (decision to update or not) 3) Write an update thread and let it do the work for you The first two alternatives could use an external boolean value or generate a boolean decision by using the timedelta between the last call und the current calling time. Third alternative would be a timed thread which runs every n seconds/minutes and load the heavy data.

提交回复
热议问题