Win a copy of Spark in Action this week in the Open Source Projects forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Scheduled check ...

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

First of all Hi to everyone, as this is my first post here !

I'm writing (trying) a simple java app that could make a db query, fetch result and display message on the screen ... and do it every 5 min .
I've got everything working except those 5 min ...
I need to pause timer somehow, and start next delay (period) again only after a button has been pressed.
Now if I don't press OK button for let's say 2h, I'll need to press it about 24 (2h/5min delay) times ...
I hope I made myself clear ...

Can anyone give me an advice?
Is java.util.Timer a good solution ?

This is how my code looks like now :




Kuba
 
author
Posts: 3281
8
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kuba and welcome to Javaranch! Can I ask what version of the JDK you are using?
 
Kuba Bernatowicz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Sorry, I forgot about it.

JDK1.6.0_07
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you should take a look at the java.util.concurrent package and the class ScheduledExecutorService.

There you find what you are looking for. Using timers is "old school".

Also... never EVER do any real work in the main-method. That method is there to initiate the real work and should only contain the code needed to start your application (reading application properties, setting up loggers, setting up data sources and stuff like that).
[ December 05, 2008: Message edited by: Ove Lindstr�m ]
 
Kuba Bernatowicz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ove,

I googled :
http://gallemore.blogspot.com/2007/05/java-threading-scheduledexecutorservice.html

and last example is just what I needed.
 
bacon. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic