Java实现定时器Timer-Thread.sleep()-ScheduledTask -Quartz

定时关闭while循环

先定义一个开关

private static boolean flag;

 设置timer定时运行的任务,用来定时一分钟关闭while循环

private static TimerTask timerTask;

//传值true表示Timer线程为守护线程,主线程结束跟随结束,默认false
Timer timer = new Timer(true);

实际上就是一个Runnable
timerTask = new TimerTask() {
    @Override
    public void run() {
        flag = false;
        timerTask.cancel();
        timer.cancel();
        timer.purge();
    }
};
timer.schedule(timerTask,60*1000); 
while (flag) {

//业务实现...

}

 

timer.schedule(timerTask,60*1000); 

60秒后执行一次

timer.schedule(timerTask,10*1000,60*1000); 

10秒后执行第一次,然后每隔60秒执行一次。

可用Calendar 设置时间

Calendar calendar = new GregorianCalendar();
       calendar.add(Calendar.SECOND, 5);
        Date runDate = calendar.getTime();
        MyTask myTask = new MyTask(); 
Timer timer = new Timer();
timer.schedule(myTask, runDate);

class MyTask extends TimerTask {

    @Override
    public void run() {
        System.out.println("MyTask运行时间:" + new Date());
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Timer常用方法

schedule(TimerTask task, long delay);//指定任务task,在delay毫秒延迟后执行
schedule(TimerTask task, Date time);//指定任务task,在time时间点执行一次
schedule(TimerTask task, long delay, long period);//指定任务task,延迟delay毫秒后执行第一次,并在之后每隔period毫秒执行一次
schedule(TimerTask task, Date firstTime, long period);//指定任务task,在firstTime的时候执行第一次,之后每隔period毫秒执行一次
scheduleAtFixedRate(TimerTask task, long delay, long period);//作用与schedule一致
scheduleAtFixedRate(TimerTask task, Date firstTime, long period);//作用与schedule一致
实际上最后都会使用sched(TimerTask task, long time, long period);//即指定任务task,在time执行第一次,之后每隔period毫秒执行一次

 schedule使用系统时间计算下一次,即System.currentTimeMillis()+period
而scheduleAtFixedRate使用本次预计时间计算下一次,即time + period
对于耗时任务,两者区别较大,请按需求选择,瞬时任务无区别。

简单Thread.sleep()实现延迟

new Thread(() -> {
    while (count < 10) {
        System.out.println(new Date().toString() + ": " + count);
        count++;
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}).start();

线程池

threadPool.execute(() -> {
    while (count < 10) {
        System.out.println(new Date().toString() + ": " + count);
        count++;
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
});

 使用ScheduledTask 

//设置间隔时间,每2秒执行一次 
taskScheduler.scheduleAtFixedRate(() -> {
    System.out.println(new Date().toString() + ": " + count+"设置间隔时间2秒");
    count++;
}, 2000);
//可以用Cron表达式,每天凌晨1点执行
taskScheduler.schedule(() -> {
    System.out.println(new Date().toString() + ": " + count+"凌晨1点执行");
    count++;
}, new CronTrigger("0 0 1 * * ?"));

corn表达式生成地址:https://cron.qqe2.com/ 

//设置间隔时间,每5.5秒执行一次
taskScheduler.scheduleAtFixedRate(() -> {
    System.out.println(new Date().toString() + ": " + count+"设置间隔时间5.5秒");
    count++;
}, 5500);

Quartz

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.3.2</version>
</dependency>
//创建定时任务的对象
public class MyQuartz implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        Date date = new Date();
        String format = new SimpleDateFormat("yy-MM-dd HH-mm-ss").format(date);
        System.out.println("======响应时间为:"+format);
    }
}
//创建执行类
@Component
public class MySchedule {
    public MySchedule() throws SchedulerException {
        doQ();
    }
    public void doQ() throws SchedulerException {
        //1、调度器Scheduler的创建
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        //2、创建JobDetail实例,并绑定执行类
        JobDetail job = JobBuilder.newJob(MyQuartz.class).withIdentity("Quartz1", "QuartzGroup1").build();
        //3、构建Trigger实例
        CronTrigger build = TriggerBuilder.newTrigger().withIdentity("Trigger1", "QuartzTrigger1")
                .startNow()
                //corn表达式指,2秒后开始执行,每3秒执行一次。
                .withSchedule(CronScheduleBuilder.cronSchedule("2/3 * * * * ? *"))
                .build();
        //4、执行Quartz
        scheduler.scheduleJob(job,build);
        scheduler.start();
    }

//5.利用TimeUnit实现延时操作
        TimeUnit.SECONDS.sleep(30);
        //6.关闭定时任务
        scheduler.shutdown();
        System.out.println("定时任务结束!");

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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及各代码文件中注释

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今晚哒老虎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值