zoukankan      html  css  js  c++  java
  • java当中的定时器的4种使用方式


    import
    java.util.Calendar;
    2
    import java.util.Date;
    3
    import java.util.Timer;
    4
    import java.util.TimerTask;
    5
    
    
    6
    public class TimeTest {
    7
      public static void main(String[] args) {
    8
        timer1();
    9
        //timer2();
    10
        //timer3();
    11
        //timer4();
    12
      }
    13
    
    
    14
      // 第一种方法:设定指定任务task在指定时间time执行 schedule(TimerTask task, Date time)
    15
      public static void timer1() {
    16
        Timer timer = new Timer();
    17
        timer.schedule(new TimerTask() {
    18
          public void run() {
    19
            System.out.println("-------设定要指定任务--------");
    20
          }
    21
        }, 2000);// 设定指定的时间time,此处为2000毫秒
    22
      }
    23
    
    
    24
      // 第二种方法:设定指定任务task在指定延迟delay后进行固定延迟peroid的执行
    25
      // schedule(TimerTask task, long delay, long period)
    26
      public static void timer2() {
    27
        Timer timer = new Timer();
    28
        timer.schedule(new TimerTask() {
    29
          public void run() {
    30
            System.out.println("-------设定要指定任务--------");
    31
          }
    32
        }, 1000, 5000);
    33
      }
    34
    
    
    35
      // 第三种方法:设定指定任务task在指定延迟delay后进行固定频率peroid的执行。
    36
      // scheduleAtFixedRate(TimerTask task, long delay, long period)
    37
      public static void timer3() {
    38
        Timer timer = new Timer();
    39
        timer.scheduleAtFixedRate(new TimerTask() {
    40
          public void run() {
    41
            System.out.println("-------设定要指定任务--------");
    42
          }
    43
        }, 1000, 2000);
    44
      }
    45
       
    46
      // 第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行.
    47
      // Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period)
    48
      public static void timer4() {
    49
        Calendar calendar = Calendar.getInstance();
    50
        calendar.set(Calendar.HOUR_OF_DAY, 12); // 控制时
    51
        calendar.set(Calendar.MINUTE, 0);    // 控制分
    52
        calendar.set(Calendar.SECOND, 0);    // 控制秒
    53
    
    
    54
        Date time = calendar.getTime();     // 得出执行任务的时间,此处为今天的12:00:00
    55
    
    
    56
        Timer timer = new Timer();
    57
        timer.scheduleAtFixedRate(new TimerTask() {
    58
          public void run() {
    59
            System.out.println("-------设定要指定任务--------");
    60
          }
    61
        }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
    62
      }
    63
    }
  • 相关阅读:
    微服务使用总结
    Xftp的下载安装,以及如何使用XFtp连接虚拟主机/服务器
    "net.sf.hibernate.PropertyValueException"
    "net.sf.hibernate.PropertyValueException"
    "net.sf.hibernate.PropertyValueException"
    XMLHttpRequest 请求java部署的webservice 跨域问题
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/lantianxun/p/timer.html
Copyright © 2011-2022 走看看