zoukankan      html  css  js  c++  java
  • Java定时器TimeTask

    package com.alan.timer;

    import java.util.Calendar;
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    /**
    * Java定时器
    * @author 邵海雄
    * @date 2016年9月27日 下午5:38:19
    * @version v1.0
    */
    public class TimerTest {
    // 第一种方法:设定指定任务task在指定时间time执行 schedule(TimerTask task, Date time)
    public static void timer1() {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
    public void run() {
    System.out.println("-------设定要指定任务--------");
    }
    }, 2000);// 设定指定的时间time,此处为2000毫秒
    }

    // 第二种方法:设定指定任务task在指定延迟delay后进行固定延迟peroid的执行
    // schedule(TimerTask task, long delay, long period)
    public static void timer2() {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
    public void run() {
    System.out.println("-------设定要指定任务--------");
    }
    }, 1000, 5000);
    }

    // 第三种方法:设定指定任务task在指定延迟delay后进行固定频率peroid的执行。
    // scheduleAtFixedRate(TimerTask task, long delay, long period)
    public static void timer3() {
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
    public void run() {
    System.out.println("-------设定要指定任务--------");
    }
    }, 1000, 2000);
    }

    // 第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行.
    // Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period)
    public static void timer4() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 12); // 控制时
    calendar.set(Calendar.MINUTE, 0); // 控制分
    calendar.set(Calendar.SECOND, 0); // 控制秒

    Date time = calendar.getTime(); // 得出执行任务的时间,此处为今天的12:00:00

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
    public void run() {
    System.out.println("-------设定要指定任务--------");
    }
    }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
    }

    /**
    * 每天下午17:00执行一次
    *
    * @author 邵海雄
    * @date 2016年9月27日 上午10:08:44
    * @version v1.0
    */
    public static void timer5() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 17); // 控制时
    calendar.set(Calendar.MINUTE, 0); // 控制分
    calendar.set(Calendar.SECOND, 0); // 控制秒

    Date time = calendar.getTime(); // 得出执行任务的时间,此处为今天的17:00:00

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
    public void run() {
    System.out.println("-------每日17:00执行--------");
    }
    }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
    }
    private static final int DAY=1;//日
    /**
    * 每月1号执行一次
    *
    * @author 邵海雄
    * @date 2016年9月27日 上午10:11:26
    * @version v1.0
    */
    public static void timer6() {
    final Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0); // 控制时
    calendar.set(Calendar.MINUTE, 0); // 控制分
    calendar.set(Calendar.SECOND, 0); // 控制秒
    Date time = calendar.getTime();
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
    public void run() {
    //判断当前是否是当月的1号
    if (calendar.get(Calendar.DAY_OF_MONTH)==DAY) {
    System.out.println("-------每月1号执行--------");
    }
    }
    }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
    }
    public static void main(String[] args) {
    // timer1();
    // timer2();
    // timer3();
    // timer4();
    // timer5();
    timer6();
    }
    }

  • 相关阅读:
    作为一枚第二天上班的小小.net程序员(技术宅的那种)很迷茫哦,第一个随笔
    清除NT Kernel & System占用80端口
    case then 的用法 貌似case then不支持别名
    syscomments 可以用来查找所有关于库中用到的某个关键词的所有相关脚本
    查看系统表存储过程名称SELECT *,OBJECT_NAME(id) FROM syscomments
    毫秒级百万数据分页存储过程
    使用sp_configure启用 'Ad Hoc Distributed Queries'
    使用 ServKit(PHPnow) 搭建 PHP 环境[图]
    apache+php+mysql常见集成环境安装包
    Quartz.NET作业调度框架详解
  • 原文地址:https://www.cnblogs.com/shaohaixiong/p/5913742.html
Copyright © 2011-2022 走看看