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();
        }
    }
  • 相关阅读:
    (转载)博客园如何转载别人的文章
    python实现凯撒密码、凯撒加解密算法
    python新手学习可变和不可变对象
    Pycharm中配置远程Docker运行环境的教程图解
    python新手学习使用库
    python的help函数如何使用
    python编写softmax函数、交叉熵函数实例
    python能开发游戏吗
    python属于解释语言吗
    python的控制结构之For、While、If循环问题
  • 原文地址:https://www.cnblogs.com/heyjia/p/11225672.html
Copyright © 2011-2022 走看看