I want that a particular method is called after a certain delay. I tried different aproaches likes timer, executor or handlers. They fullfill what they are supposed to do, but with a little exception. The delayed method call makes some changes in a multimap from guava. Within the run-method of the TimerTask the output is like I want it. But if I print the multimap outside of run the changes are withdrawn and I still have the old multimap values. But I need the updated ones, because my data is saved in it and I need the updated values to work with.

My code looks like that:

public class classTimer {
  public static void main(String[] args) {
    //some code, irrelevant for the task

             new Timer().schedule(new TimerTask() {          
             @Override
             public void run() {
                 dataMap = UndoManager.undoChanges(dataMap, a, hw); 
                 // Point 1  
                    }
                }, delay);  
                // Point 2
   }
 }

Like I said before, printing the dataMap at Point1 gives the correct output, at Point 2 the old values, like the Method UndoChanges was newer called. Multimaps are implemented that changes stay consistent and normally the values must have been changed, but thats not the case here. What am I missing here? If somebody knows a different method to call a method after a delay, I would be happy to hear it.

Thanks and much appreciated

有帮助吗?

解决方案

Point 2 in your code executes immediately after you schedule the undo task. This is because the schedule method returns immediately: it doesn't wait for the delay to pass and the task to be executed. That is really the point of using Timers: schedule asynchronous tasks to run at some time in the future.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top