java 定时器 的几种实现及优缺点

一:java Timer 在 java.util.Timer包中的Timer 类可以实现定时功能。
1. 使用 Timer 进行任务调度

<span style="font-size:18px;">public class TimerTest extends TimerTask { 
public TimerTest(){} </span>
<span style="font-size:18px;">@Override 
 public void run() { 
 System.out.println("定时任务处理逻辑"); 
 } </span>
<span style="font-size:18px;">public static void main(String[] args) { 
        Timer timer = new Timer(); 
        long delay1 = 1 * 1000; 
        long period1 = 1000; 
        // 从现在开始延时 1 秒钟之后开始,每隔 1 秒钟执行一次run 方法。
        timer.schedule(new TimerTest("job1"), delay1, period1); 
        long delay2 = 2 * 1000; 
        long period2 = 2000; 
</span><pre name="code" class="displaycode"><span style="font-size:18px;">        // 从现在开始延时 2秒钟之后开始,每隔 2 秒钟执行一次run 方法。
        timer.schedule(new TimerTest(), delay2, period2); 

        //指定每天的指定时间执行 定时任务(run 方法的逻辑处理)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd ");
        String nowDate = sdf1.format(new Date());
        String timerTime = "23:49:00";
        Timer timer = new Timer();
        String timerDateStr = nowDate + timerTime;
        Date timerDate = null;
        try {
            timerDate = sdf.parse(timerDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //</span><span style="font-size:18px;">timerDate时间小于当前时间则自动执行一次业务逻辑,当前时间未达到指定该参数的时间时,
        //只有达到该参数指定时间时才会执行任务
</span><span style="font-size:18px;">        timer.schedule(new </span><span style="font-size:18px;">TimerTest()</span>,<span style="font-size:18px;"> timerDate);</span>
 }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
JAVA精确定时器,利用系统时间,使长期工作的误差稳定。 功能: ·可定时启动任务或直接启动任务 ·重复启动任务(时间间隔可在任务线程中改变,范围大于100ms,否则精度降低) 引用列表: ·import psn.razerpen.thread.AccuracyTimer; ·import psn.razerpen.thread.AccuracyTimerMission; ·import psn.razerpen.time.TimeStruct; 使用方法: //1·继承AccuracyTimerMission接口,创建一个类。 class MyTimer implements AccuracyTimerMission { //2·指定一个周期 int nDelay=1000; //3·重写run方法(如不需要使用新线程执行任务,也可留空) /** * 任务线程,本函数继承自Runnable */ @Override public void run() { System.out.println(new TimeStruct()); } //4·重写RunInCurrentThread(long nCurrentMilliSecond)方法。该方法接收当前时间,并返回下一次执行的时间。如果返回值不大于nCurrentMilliSecond则中止计时器。该方法必须重写。 /** * 接收当前时间的毫秒值,并返回下一次执行的毫秒值。如果返回的下一个时间早于当前时间,则退出 */ @Override public long RunInCurrentThread(long nCurrentMilliSecond) { return nCurrentMilliSecond+=nDelay; } } //5·创建主线程代码 public class TestTimer { public static void main(String[] args) throws InterruptedException { //6·创建一个AccuracyTimer对象,并指定一个任务。 AccuracyTimer at=new AccuracyTimer(new MyTimer()); //7·(可选)如果不需要在新线程中启动任务,则写 // at.SetNewThreadEnabled(false); //否则不写或者写 // at.SetNewThreadEnabled(true); //8·(可选)设定第一次启动的时间点SetNextMissionTime/SetNextMissionMilliSecond或延迟时间SetNextMissionMilliSecondFromNow //设置为当前这一分钟的第59秒后启动(不写此行则表示直接启动) at.SetNextMissionTime(Integer.MIN_VALUE, -1, -1, -1, -1, 59, 0); //9·启动定时器 at.Start(); //10·主线程继续 for(int i=0;i<60;++i){ Thread.sleep(1000); } //11·结束定时器 at.End(); } } 详见sample.razerpen.thread包中TestTimer及各代码文件中注释

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值