zoukankan      html  css  js  c++  java
  • 多线程02-定时器

    案例1

    间隔1秒以后执行task任务

    package org.lkl.timer;
    
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class TimerFoo {
    
        public static void main(String[] args) {
            
            TimerTask task = new TimerTask() {
                
                @Override
                public void run() {
                    System.out.println("Liaokailin.");
                }
            };
            /**
             * 间隔1秒以后执行task任务
             */
            new Timer().schedule(task, 1000) ;
            
            while(true){
                System.out.println(new Date().getSeconds());
                try {
                    Thread.sleep(1000) ;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }

    案例2 

     间隔1秒以后第一次执行task ,随后间隔2秒执行一次task

    package org.lkl.timer;
    
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class TimerFoo {
    
        public static void main(String[] args) {
            
            TimerTask task = new TimerTask() {
                
                @Override
                public void run() {
                    System.out.println("Liaokailin.");
                }
            };
            /**
             * 间隔1秒以后第一次执行task ,随后间隔2秒执行一次task
             */
            new Timer().schedule(task, 1000,2000) ;
            
            /**
             * 用来计时,没间隔1秒输出当前时间对应的秒数
             */
            while(true){
                System.out.println(new Date().getSeconds());
                try {
                    Thread.sleep(1000) ;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }

     案例3

    间隔2秒以后执行一次task ,随后间隔4秒执行一次task

    package org.lkl.timer;
    
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class TimerFoo {
        static int flag = 0 ;
        public static void main(String[] args) {
        
            class MyTimerTask extends TimerTask{
                int count = ++flag%2 ;
                //flag = flag%2 ;
                @Override
                public void run() {
                    System.out.println("Liaokailin.");
                    new Timer().schedule(new MyTimerTask() , 2000+2000*count) ;
                }
                
            }
            
         
            /**
             * 间隔2秒以后执行一次task ,随后间隔4秒执行一次task
             */
            new Timer().schedule(new MyTimerTask() , 2000) ;
            
            /**
             * 用来计时,没间隔1秒输出当前时间对应的秒数
             */
            while(true){
                System.out.println(new Date().getSeconds());
                try {
                    Thread.sleep(1000) ;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }
  • 相关阅读:
    TensorFlow gfile文件操作详解
    ROS学习之日志消息
    typeid().name()获取类型名
    Ubuntu Qt配置QVTKWidget控件
    python-pcl简易文档(不包含自建函数与pcl_grabber包)
    ros源码之初始化函数init()调用的几个初始化函数
    奇异值分解(SVD)原理
    ROS节点的初始化及退出详解(ros::init、SIGINT、ros::ok、ros::NodeHandle)
    ROS Nodehandle句柄
    C++可变参数模板
  • 原文地址:https://www.cnblogs.com/liaokailin/p/3770901.html
Copyright © 2011-2022 走看看