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

    public class TimerTest {
        //第一种方法:设定指定任务task在指定时间time后执行
        //schedule(timertask,Date time)
        public static void timer1(){
            Timer timer = new Timer();
            timer.schedule(new TimerTask(){
                public void run(){
                    System.out.println("-------timer1 test -----");
                }
            }, 5000);
        }
        //第二种方法:设定指定任务task在指定延迟delay后进行固定延迟period周期的进行
        //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("-------timer2 test -----");
                }
            }, 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("-------timer3 test -----");
                }
            }, 1000,5000);
        }
        //第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行.
        // Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period)
        public static void timer4(){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR, 14);
            calendar.set(Calendar.MINUTE,30);
            calendar.set(Calendar.SECOND,00);
            Date time = calendar.getTime();
            Timer timer = new Timer();
            timer.scheduleAtFixedRate(new TimerTask(){
                public void run(){
                    System.out.println("-------timer4 test -----");
                }
            },time,1000*60*60*24);
        }
        
        public static void main(String []args){
            timer1();
            timer4();
        }
    }
  • 相关阅读:
    sql where 与 having的区别
    Linux下shell脚本监控Tomcat的状态并实现自动启动
    CentOS MySQL自动备份shell脚本
    HTTP请求时connectionRequestTimeout 、connectionTimeout、socketTimeout三个超时时间的含义
    Linux下搭建禅道项目管理软件
    JMeter压力测试及并发量计算-2
    JMeter压力测试及并发量计算-1
    Eclipse常用快捷键大全
    常用的adb命令
    js-消息对话框
  • 原文地址:https://www.cnblogs.com/heyjia/p/11225672.html
Copyright © 2011-2022 走看看