基于 Timer是一种定时器工具

没有依赖   

通过Timer中的schedule方法启动定时任务

一般不采用此方法
/**
 * -------------------------------------------------------------*
 *                     COPYRIGHT(C) 2018                        *
 *   National Audit Office of the People’s Republic Of China    *
 *                                                              *
 *                                                              *
 *  This work contains confidential business information        *
 *  and intellectual property of CNAO.                          *
 *  All rights reserved.                                        *
 * -------------------------------------------------------------*
 */
/****************************************************************
 * Revision information:
 *
 *@version    1.0    2019年4月3日    Initial release (ChenJunMa)
 *
 ***************************************************************/
package tt.tt;

import org.springframework.stereotype.Service;


import java.util.Timer;
import java.util.TimerTask;


/**
 *Timer是一种定时器工具,用来在一个后台线程计划执行指定任务。它可以计划执行一个任务一次或反复多次。
 * TimerTask一个抽象类,它的子类代表一个可以被Timer计划的任务。具体的任务在TimerTask中run接口中实现。
 * 通过Timer中的schedule方法启动定时任务。
 * ---------------------
 *
 *
 */
@Service
public class TimersDemo {

    private static Timer timer = new Timer();

    public TimersDemo() {
        TaskDemo task = new TaskDemo();
        timer.scheduleAtFixedRate(task, 10 * 1000, 10 * 1000);
        System.err.println("开始计时啦 宅男们 ...");
    }

    private class TaskDemo extends TimerTask {

        @Override
        public void run() {
            //开始调度啦
            System.err.println("你是笨笨猪呀--笨笨猪呀--笨笨猪呀--笨笨猪呀");
            System.out.println(getName("jb"));

        }

        private String getName(String str){
            return "毛毛哥 皮皮哥 宝强哥";
        }

    }


}
View Code

猜你喜欢

转载自www.cnblogs.com/JonaLin/p/11250613.html