• Post Reply Bookmark Topic Watch Topic
  • New Topic

Set 10 min timer for repeat  RSS feed

 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a client side program that grabs information about the computer it runs on. I want to have it grab the same info every so often, and check it against the original.
what can be used to do something like that? end game would be having it start up with the pc, then check periodically. if the values are different, send them to the database
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Joseph wrote:what can be used to do something like that?

Timer and TimerTask
 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
awesome! thank you very much. i had googled timer, but i wasn't seeing anything about timertask.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option is ScheduledThreadPoolExecutor.
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use threads. In this example, a thread does the querying (simulated), sleeps 10 seconds, and repeats. The thread ends when you call interrupt(). Here's the code:
 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks pawel and edwin! to be honest, ive been a little apprehensive about tackling the thread thing, but ill check it out. it just seems really difficult
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Joseph wrote:i have a client side program that grabs information about the computer it runs on. I want to have it grab the same info every so often, and check it against the original.

Just to add to the pot - and depending on what "information" you need to check - you might also want to have a look at the WatchService API (the tutorial is here).

It might also be worth mentioning that there are two models for doing this sort of stuff: 'passive' and 'active', and the one you've described is "passive" - Do some action, wait a while, rinse and repeat.

An 'active' model is more like a "listener" (or Observer), that gets notified every time something happens to the "thing" it's observing. The advantages of this style are that:
1. It can often be implemented without threading.
2. It can be much more "responsive".
3. It avoids unnecessary processing.

A possible problem with your approach is that you've arbitrarily chosen 10 minutes as your "cycle" time, which may have nothing to do with the way your system actually gets updated, resulting in a lot of instances where your "checker" does a lot of work simply to discover that nothing's changed; whereas an 'active' checker will only notify your "listener" when something has changed.

You may still decide to go with a passive pattern, and that's fine; but it's worth knowing about other options before you make your final decision.

Winston
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwin Torres wrote:You can use threads. In this example, a thread does the querying (simulated), sleeps 10 seconds, and repeats. The thread ends when you call interrupt(). Here's the code:


That's the way I solved problems like these before using the Timer / TimerTask. Are there any potential downsides to this? I think code like this is in a no-longer maintained program of mine that might or might not run on a client's server ;)
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:That's the way I solved problems like these before using the Timer / TimerTask. Are there any potential downsides to this? I think code like this is in a no-longer maintained program of mine that might or might not run on a client's server ;)

It made sense to me to use another thread to do the querying. But maybe it's additional overhead for this example? Another downside is synchronization. When multiple threads access the same object, you need to synchronize. Winston suggests that a single thread that waits, executes, and repeats could be enough. That may be true. The decision to use threads depends on what else the program needs to do. I just wanted to provide another option. :-)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwin Torres wrote:Winston suggests that a single thread that waits, executes, and repeats could be enough.

Actually, what you're describing is precisely what a "passive" pattern does. It sits, waits, and then does something. I don't know the terminology these days, but it used to be called "busy waiting", and it tends to consume CPU cycles (although probably less these days than it used to). But the fact of the matter is that it's simply a process that sits and "whirrs" while it's waiting for something to do.

The whole business of an active "Observable" is that it informs the "observer" object (which may not be doing anything at all), by a direct call to one of its methods, that there is NOW something that it needs to react to. What the "observer" does then is up to it; but there is no "whirring", and no "sitting and waiting" - except in the sense that any loaded class/object may be "waiting" to be used. The listener object reacts when - and ONLY when - one of its methods has been called directly.

Hope it makes some sense.

Winston
 
Matthew Joseph
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks everyone. you have given me a lot of stuff to research
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Joseph wrote:thanks everyone. you have given me a lot of stuff to research

Glad we could help.

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