• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a method after 10 seconds - X post  RSS feed

 
Mike Anna
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Hope for some help.
I need to call a method after 10 seconds. That is to make sure that if one particular field is updated, say 3 times in a window of 10 seconds, I should just be able to take the last value and process it, in my ajax app. I am so far using the Timer class, but the problem is, it ticks off a thread for every single request to be processed after 10 seconds and processes all the 3 requests, where as I should just be running that method once for the last request. Could you please help me with this ?
Cant do this at the client level, for the page may be closed within those 10 seconds of the event and the setTimeout wont work then.
This is what I made so far.:



Also, I looked at examples for the swing timer ultimately takes in a ActionEvent parameter. I have none since this is being done in a in a web app.

I tried using a boolean flag for this. However, the timer code expects only final types.

cheers..
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the value in the Map removed once the change has been processed? If so, then if this is the first request in the 10 second span, you would expect the Mapped value to be null, until you do

So you could use that as a signal - if the put returns null, then add the timer. If if returns non-null don't bother. You would then want to remove the value from the map during processing. And you probably want to use a safer Map for multi-threaded used - HashMap may cause problems if you try to add and get/remove simulteneously. You might try using Collections.synchronizedMap() or one of the ConcurrentMap implementations to make it safe to use in multiple threads.
 
Mike Anna
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
Well, I dont know how, but just now it struck me, the only stupid thing I was doing was that I wasnt 'removing' the freaking entry from the map. So now, even if that silly timer runs thrice, it doesnt matter for there is nothing in the map.
Thanks for your advice on the multi threading thing. Now I use a collections.synchronisedMap.

thanks for your help

cheers...

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!