【问题标题】:Java timer to make a function run every minuteJava计时器使函数每分钟运行一次
【发布时间】:2014-05-03 13:35:00
【问题描述】:

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

【问题讨论】:

  • 为了更容易获得答案,添加您尝试过的代码之类的详细信息以及您到目前为止对用户输入数据时延迟问题的理解将是有益的。有了这些信息,我们就可以确切地知道我们需要提供什么来解决您的问题,并且可以更快地提供更好的答案。谢谢!

标签: java multithreading timer


【解决方案1】:

看看programcreek.com上wait()和notify()的these examples,你知道wait和notify的概念吗?

【讨论】:

    【解决方案2】:

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

    在新界面中:

     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()。延迟也是通过构造函数设置的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      相关资源
      最近更新 更多