timer计时器 java_java Timer类的使用(计时器)

/*

* 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秒执行一次

* */

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值