Jump to content
xisto Community
Sign in to follow this  
T X

Timer Api For processing

Recommended Posts

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://forums.xisto.com/no_longer_exists/
http://forums.xisto.com/no_longer_exists/

Share this post


Link to post
Share on other sites

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

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.


Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.