java 无限延时处理类封装

77 阅读1分钟

无限延时

public class TimeUtils {

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            schedule("test", () -> {
                System.out.println("test");
            }, 3000);
        }
        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static ConcurrentHashMap<String, TimeUtils> TimeUtilsMap = new ConcurrentHashMap<>();

    private TimerTask timerTask;

    private Timer timer;

    private TimeFunction function;

    private long scheduleTime;

    /**
     * 无限延时处理类
     *
     * @param scheduleName 任务名称
     * @param function     需要执行逻辑
     * @param scheduleTime 延时时间
     * @return 当前所有的延时任务
     */
    public static ConcurrentHashMap<String, TimeUtils> schedule(String scheduleName, TimeFunction function, long scheduleTime) {
        try {
            TimeUtils timeUtils = TimeUtilsMap.get(scheduleName);
            if (timeUtils == null) {
                TimeUtilsMap.put(scheduleName, new TimeUtils(function, scheduleTime));
            } else {
                timeUtils.schedule(scheduleTime, function);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return TimeUtilsMap;
    }

    public TimeUtils(TimeFunction function, long scheduleTime) {
        this.function = function;
        this.scheduleTime = scheduleTime;
        this.timer = new Timer();
        schedule(scheduleTime, function);
    }

    public void schedule(long scheduleTime, TimeFunction function) {
        try {
            if (this.timerTask != null) {
                this.timerTask.cancel();
            }
            if (this.timer == null) {
                this.timer = new Timer();
            }
            this.timerTask = new TimerTask() {
                @Override
                public void run() {
                    try {
                        function.run();
                        timer.cancel();
                        timer = null;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            timer.schedule(timerTask, scheduleTime);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public TimeFunction getFunction() {
        return function;
    }

    public void setFunction(TimeFunction function) {
        this.function = function;
    }

    public long getScheduleTime() {
        return scheduleTime;
    }

    public void setScheduleTime(long scheduleTime) {
        this.scheduleTime = scheduleTime;
    }

    public TimerTask getTimerTask() {
        return timerTask;
    }

    public void setTimerTask(TimerTask timerTask) {
        this.timerTask = timerTask;
    }

    public Timer getTimer() {
        return timer;
    }

    public void setTimer(Timer timer) {
        this.timer = timer;
    }
}
public interface TimeFunction {

    void run();

}