简体   繁体   English

Java计时器使功能每分钟运行

[英]Java timer to make a function run every minute

I have an application where I need to access a webservice every minute. 我有一个需要每分钟访问一次Web服务的应用程序。 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 我不太明白如何在Java中实现

在programcreek.com上查看这些 wait()和notify()的示例,您知道等待和通知的概念吗?

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. 我个人是这样处理的:我声明2个类,这些类将在给定的毫秒后为您提供函数调用的功能。

In a new interface: 在新界面中:

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

And in another class 在另一堂课

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;
        }
    }
}

All you have to do is to create an object of TickManager , register a TickListener in it and call startTicking() on it. 您要做的就是创建一个TickManager对象,在其中注册一个TickListener在其上调用startTicking() The delay is set via constructor also. 延迟也是通过构造函数设置的。

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. 可以通过调用getState()函数并将其与类常量进行比较来进行检查。 If it is, then you must stop it, change the delay and start it again. 如果是,则必须将其停止,更改延迟,然后重新启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM