Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Timer Api


2 replies to this topic

#1 T X

    Member [Level 1]

  • Kontributors
  • PipPipPipPip
  • 56 posts
  • Gender:Male
  • Location:Rochester, NY
  • myCENT:1.63

Posted 27 March 2010 - 09:47 PM

import java.util.Timer;
import java.util.TimerTask;

public class TimerTest {
	
	public TimerTest() {
		getTimer().scheduleAtFixedRate(
				new TimerTask() {
					@Override
					public void run() {
						System.out.println("It has been 5 seconds.");
					}
				},
		5000, 5000);
	}
	
	public static void main(String[] args) {
		new TimerTest();
	}
	
	public Timer getTimer() {
		return timer;
	}
	
	private Timer timer = new Timer();

}
The timer will wait 5 seconds, then display the message, then continuously wait 5 seconds and display the message over and over again. I actually think this is kind of cool since I didn't know about it before.

Documentation: http://java.sun.com/...util/Timer.html
http://java.sun.com/j2se/1.4.2/docs/api/ja.../TimerTask.html

#2 manish-mohania

    Member [Level 1]

  • Kontributors
  • PipPipPipPip
  • 55 posts

Posted 28 March 2010 - 05:44 AM

yup, it is cool ...

I found it useful for some tasks that require scheduling, like generating reports at regular intervals.

There are open source api(s) for scheduling like Quartz, they provide additional features like:
1. you can provide time interval in unix cron like format.
2. Persistent jobs

Quote


For J2ee applications, EJB's do not allow to create a new thread. TimerTask is a thread, so EJB's have their own way of scheduling tasks.

Quote



#3 T X

    Member [Level 1]

  • Kontributors
  • PipPipPipPip
  • 56 posts
  • Gender:Male
  • Location:Rochester, NY
  • myCENT:1.63

Posted 29 March 2010 - 07:30 PM

Yeah I have heard of Quartz however I never took the time to look into it.

I usually use the Timer to process changes or repaint in my games so it's actually useful to me.




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users