zoukankan      html  css  js  c++  java
  • JAVA定时任务

    第一种方式:

    spring框架,编写定时任务类,构造函数里使用Timer创建定时任务,然后扫描该类所在jar包

    package scheduledTasks;
    
    import java.util.Timer;
    import java.util.TimerTask;
    import org.springframework.stereotype.Component;
    
    @Component
    public class TimedTask {
    
        TimedTask(){
            try {
                //第一个参数是任务,第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间,时间单位是毫秒
                new Timer().scheduleAtFixedRate(new TimerTask() {
                    @Override
                    public void run() {
                        System.out.println("定时任务执行");
                    }
                }, 0L, 1000L);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    第二种方式:使用java.util.concurrent.ScheduledThreadPoolExecutor结合org.apache.commons.lang3.concurrent.BasicThreadFactory(推荐使用)

    package haiyang.yu.timer;
    
    import org.apache.commons.lang3.concurrent.BasicThreadFactory;
    
    import java.util.Date;
    import java.util.concurrent.ScheduledThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
    /**
     * Created on 2018-04-20 13:09
     * <p>Title:  haiyang.yu.timer</p>
     * <p>
         使用这种方案,需要引入common-lang3的jar包
         <dependency>
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-lang3</artifactId>
             <version>3.6</version>
         </dependency>
     * </p>
     *
     * @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
     * @version 1.0
     */
    public class TimedTask {
    
        private static void useScheduledThreadPoolExecutorImplTimedTask(){
            ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(
                    1, new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(false).build());
            // 第一个参数是任务,第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间,第四个参数是时间单位
            scheduledThreadPoolExecutor.scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    System.out.println("Local Time is " + new Date().toString());
                }
            }, 0L, 1L, TimeUnit.SECONDS);
        }
    
        public static void main(String[] args) {
            useScheduledThreadPoolExecutorImplTimedTask();
        }
    }

    第三种方式:使用java.lang.Thread,这种方式使用的最多,但也是最lower的。

    package haiyang.yu.timer;
    
    import java.util.Date;
    
    /**
     * Created on 2018-04-20 13:09
     * <p>Title:  haiyang.yu.timer</p>
     * <p>Description: </p>
     *
     * @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
     * @version 1.0
     */
    public class TimedTask {
    
        private static void useThreadImplTimedTask(){
    
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        
                        System.out.println("Local Time is " + new Date().toString());
                        
                        try {
                            //时间间隔,单位是毫秒
                            Thread.sleep(1000L);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            
            Thread thread = new Thread(runnable);
            thread.start();
    
        }
    
        public static void main(String[] args) {
            useThreadImplTimedTask();
        }
    }
    个人理解,如有错误,欢迎指正!
  • 相关阅读:
    tips
    【十大算法实现之KNN】KNN算法实例(含测试数据和源码)
    智力趣题几则
    JAVA知多少
    R语言(入门小练习篇)
    文本分类,数据挖掘和机器学习
    推荐系统的循序进阶读物(从入门到精通)
    【贪心】PAT 1033. To Fill or Not to Fill (25)
    博弈故事一则——海盗分金币问题
    基于WordNet的英文同义词、近义词相似度评估及代码实现
  • 原文地址:https://www.cnblogs.com/gllegolas/p/11851029.html
Copyright © 2011-2022 走看看