[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

Timer for Java

I would like to have a timer in my Java Applet that polls a website every prescribed time. Is there a property or code that I could use?
1 Solution
The easiest way to do this is to set up the process that is doing the polling to go to sleep and then wake up every specified interval and poll the site.

Even better, put this code into a separate thread to keep it out of the way.

You can do it like this:

while (true) {
  try {
    thread.sleep(1000 * secs);
  } catch {InterruptedException e) {)
  //poll the website and process data
Using Timers to poll every second

Timer timer = new Timer( 1000, new ActionListener
public void actionPerformed( ActionEvent e )
    //poll server
timer.setRepeats( true );
BarkleyAuthor Commented:
How do I create a separate thread?Because when I do the sleep, I cannot click any other buttons while the application  is running.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now