Java计时器使函数每分钟运行一次 [英] Java timer to make a function run every minute

查看:30
本文介绍了Java计时器使函数每分钟运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我需要每分钟访问一次网络服务.我打算为此使用一个线程,但是如果用户输入任何内容,则需要延迟计时器".我不太明白如何在java中实现这一点

I have an application where I need to access a webservice every minute. I was going to use a thread for this, however if the user enters anything, the 'timer' needs to be delayed. I don't quite understand how to implement this in java

推荐答案

我个人是这样处理的:我声明了 2 个类,它们将在每给定毫秒后为您提供函数调用的功能.

Personally here is how I deal with it: I declare 2 classes that will give you the functionality of function call after every given milliseconds.

在新界面中:

 public interface TickListener {
        public void tick();
        public void tick(Exception e);
    }

在另一个班级

public class TickManager {

    public static final int STATE_TICK_ONCE = 0;
    public static final int STATE_TICK_NONSTOP = 1;
    public static final int STATE_TICK_NONE = 2;

    int state, delay;
    Thread t;
    TickListener[] listeners;
    long tickCount;

    public TickManager(){
        this(TickManager.STATE_TICK_NONE);
    }
    public TickManager(int state){
        this(state,1000);
    }
    public TickManager(int state, int d){
        this(state,d,null);
    }
    public TickManager(int state, int d, TickListener[] t){
        this.state = state;
        delay = d;
        listeners = t;
        tickCount=0;
        if(state!=2){
            startTicking(state);
        }
    }

    public void startTicking(int s){
        tickCount = 0;
        state=s;
        t = new Thread(new Runnable() {

            @Override
            public void run() {
                while (state!=TickManager.STATE_TICK_NONE) {
                    tickCount++;
                    try {
                        Thread.sleep(delay);
                    } catch (Exception e) {
                        process(e);
                        stopTicking();
                    }
                    process();
                    if (state == TickManager.STATE_TICK_ONCE && tickCount == 1) {
                        stopTicking();
                    }
                }
            }
        });
        t.start();
    }
    public void process(){
        if(listeners!=null){
            for(TickListener l : listeners){
                l.tick();
            }
        }
    }
    public void process(Exception e){
        if(listeners!=null){
            for(TickListener l : listeners){
                l.tick(e);
            }
        }
    }
    public void stopTicking(){
        state = TickManager.STATE_TICK_NONE;
        t=null;
    }

    public int getState() {
        return state;
    }
    public int getDelay() {
        return delay;
    }
    public Thread getThread() {
        return t;
    }
    public TickListener[] getListeners() {
        return listeners;
    }
    public long getTickCount() {
        return tickCount;
    }

    public void setState(int state) {
        this.state = state;
    }
    public void setTickCount(long tickCount) {
        this.tickCount = tickCount;
    }
    public void setDelay(int delay) {
        this.delay = delay;
    }

    public void addTickListener(TickListener t){
        if(listeners!=null){
            int l = listeners.length;
            TickListener[] tl = new TickListener[l+1];
            System.arraycopy(listeners, 0, tl, 0, l);
            tl[l] = t;
            listeners=tl;
        }
        else{
            listeners=new TickListener[1];
            listeners[0]=t;
        }
    }
}

您所要做的就是创建一个TickManager 对象,在其中注册一个TickListener 并在其上调用startTicking().延迟也是通过构造函数设置的.

All you have to do is to create an object of TickManager, register a TickListener in it and call startTicking() on it. The delay is set via constructor also.

只要确保您在滴答作响时不要更改延迟时间即可.可以通过调用 getState() 函数并将其与类常量进行比较来检查它.如果是,那么您必须停止它,更改延迟并重新启动.

Just ensure that you do not change the delay time while it is ticking. It can be checked by calling getState() function and comparing it to the class constants. If it is, then you must stop it, change the delay and start it again.

这篇关于Java计时器使函数每分钟运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆