See the following classes : -
import java.util.Timer;
/**
*
* @author prasobh
*/
public class MyMain {
public MyMain(){
startTimerTask();
}
public static void main(String[] args) {
new MyMain();
}
public void startTimerTask(){
//per 3 sec task will execute
int timeInMillisec = 3000;
Timer t=new Timer();
MyClassToShedule obj=new MyClassToShedule();
t.schedule(obj, 0,timeInMillisec);
}
}
import java.util.Date;
import java.util.TimerTask;
/**
*
* @author prasobh
*/
public class MyClassToShedule extends TimerTask{
@Override
public void run() {
System.out.println("Executed @"+new Date(System.currentTimeMillis()));
}
}
run MyMain.java
Executed @Tue Apr 12 17:34:25 IST 2011
Executed @Tue Apr 12 17:34:28 IST 2011
Executed @Tue Apr 12 17:34:31 IST 2011
Executed @Tue Apr 12 17:34:34 IST 2011
Executed @Tue Apr 12 17:34:37 IST 2011
Executed @Tue Apr 12 17:34:40 IST 2011
MyMain.java
import java.util.Timer;
/**
*
* @author prasobh
*/
public class MyMain {
public MyMain(){
startTimerTask();
}
public static void main(String[] args) {
new MyMain();
}
public void startTimerTask(){
//per 3 sec task will execute
int timeInMillisec = 3000;
Timer t=new Timer();
MyClassToShedule obj=new MyClassToShedule();
t.schedule(obj, 0,timeInMillisec);
}
}
MyClassToShedule.java
import java.util.Date;
import java.util.TimerTask;
/**
*
* @author prasobh
*/
public class MyClassToShedule extends TimerTask{
@Override
public void run() {
System.out.println("Executed @"+new Date(System.currentTimeMillis()));
}
}
run MyMain.java
output:
Executed @Tue Apr 12 17:34:25 IST 2011
Executed @Tue Apr 12 17:34:28 IST 2011
Executed @Tue Apr 12 17:34:31 IST 2011
Executed @Tue Apr 12 17:34:34 IST 2011
Executed @Tue Apr 12 17:34:37 IST 2011
Executed @Tue Apr 12 17:34:40 IST 2011
No comments:
Post a Comment