八、定时调度

 1 public class TestTimer {
 2     public static void main(String[] args) {
 3         //(1)创建Timer的对象
 4         Timer t=new Timer();
 5         //(2)调用schedule()方法去执行任务
 6         //创建任务类的对象
 7         TimerTask task=new Clock();
 8         //要执行的任务,第二个参数是任务的执行开始时间 ,第三个参数是每隔多长时间执行一次
 9         t.schedule(task, new Date(System.currentTimeMillis()+1000), 1000);
10     }
11 }
12 class Clock extends TimerTask{
13     long time=1000;//1秒
14     @Override
15     public void run() {
16         Date date=new Date(time);
17         System.out.println(date.toLocaleString());
18         time+=1000;
19     }
20 }

猜你喜欢

转载自www.cnblogs.com/qiaoxin11/p/12721723.html