• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Calling a method after 10 seconds - X post

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic