/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
//import java.util.Timer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
/**
*
* @author Administrator
*/
public class TimerTest {
public static void main(String[] args) throws ParseException
{
Timer timer=new Timer();
TimerTask task=new TimerTask(){ //定义一个任务,并实现TimerTask类的抽象方法ruan()
public void run()
{
//执行需要运行的方法
System.out.println("我被执行了!");
}
};
/* 方法1:
timer.schedule(task,0,4000);//每隔4秒执行一次(0表示在开始调用task任务后多久第一次去执行task的run方法,4000表示从第二次开始每隔多久执行run方法)
// 当不需要再重复执行后需要用 task.cancel() 来结束
*/
/*方法2:
timer.schedule(task,(new SimpleDateFormat().parse("2010-12-24 下午 2:02:50")));//到指定时间执行,如果当前时间已经超过了指定时间则马上执行
//当task执行后才能用 task.cancel() 来结束任务
*/
/*方法3:
timer.schedule(task,4000);//在当前时间的4秒后执行task
//当task执行后才能用 task.cancel() 来结束任务
*/
/*方法4:
timer.scheduleAtFixedRate(task,(new SimpleDateFormat().parse("2010-12-24 下午 2:02:50")),4000);
//第一次执行时间为2010-12-24 下午 2:02:50,然后第二次开始每隔4秒执行一次
* */
}
}