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();
        }
    }
  • 相关阅读:
    iOS开发网络篇—搭建本地服务器
    iOS开发网络篇—网络编程基础
    iOS开发多线程篇—基础知识 NSOperation
    iOS开发多线程篇—NSOperation基本操作
    iOS开发多线程篇—单例模式(ARC)
    iOS开发之多线程
    iOS开发多线程篇—GCD介绍
    iOS开发多线程篇—线程安全
    Object-C非正式协议与正式协议的区别
    objective-c中Category类别(扩展类)专题总结
  • 原文地址:https://www.cnblogs.com/heyjia/p/11225672.html
Copyright © 2011-2022 走看看