如何使用Timer类调用方法,执行某些操作,重置计时器,重复? [英] How to use Timer class to call a method, do something, reset timer, repeat?

查看:552
本文介绍了如何使用Timer类调用方法,执行某些操作,重置计时器,重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名Java初学者,并且一直在寻找解决这个问题的各种解决方案,并且已经让自己陷入困境。我已经尝试过使用Threads,然后发现了这个Timer类,并且到目前为止还没有成功。如果您可以使用主要方法发布可执行代码,那么我可以看到它正常工作并从那里开始玩,那就太好了。

I'm a Java beginner and have been futzing around with various solutions to this problem and have gotten myself kind of knotted up. I've tried with Threads and then discovered this Timer class and have messed around with it without success so far. If you could post executable code with a main method so I could see it working and start playing around from there, that would be great.


  1. 启动计划

  2. 致电 doSomething()

  3. 随机生成这个长的数字和设置计时器。

  4. 当计时器关闭时,再次打电话给 doSomething()

  1. Launch program
  2. call doSomething()
  3. Generate random number and set Timer for that long.
  4. When Timer goes off, call doSomething() again.

可能使用这个: http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html

推荐答案

如果你想简单地使用Timer,我会这样做:

If you want to simply use Timer, I would do something like this:

public class TestClass {
    public long myLong = 1234;

    public static void main(String[] args) {
        final TestClass test = new TestClass();

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                test.doStuff();
            }
        }, 0, test.myLong);
    }

    public void doStuff(){
        //do stuff here
    }
}

对于糟糕的认同感到抱歉。

Sorry for the lousy identation.

此外,如果您需要安排执行代码,请看看番石榴服务,因为它可以让您的代码更加清晰和抽象创建线程,调度等的样板文件。

Also, if you need to schedule execution of code, take a look at Guava Services since it can really make your code much clearer and abstract quite a bit of the boilerplate of creating threads, scheduling, etc.

顺便说一下,我没有麻烦生成随机数等,但我认为你可以想象如何包括该部分。我希望这足以让你走上正轨。

By the way, I didn't take the trouble of generating random number, etc, but I think you can figure out how to include that part. I hope this is enough to get you on the right track.

为了记录,如果你要使用Guava,它看起来像这样:

For the record, if you were to use Guava, it would look something like this:

class CrawlingService extends AbstractScheduledService {

    @Override
    protected void runOneIteration() throws Exception {
        //run this alot
    }

    @Override
    protected void startUp() throws Exception {
        //anything you need to step up
    }

    @Override
    protected void shutDown() throws Exception {
        //anything you need to tear down
    }


    @Override
    protected Scheduler scheduler() {
        return new CustomScheduler() {
            @Override
            protected Schedule getNextSchedule() throws Exception {
                long a = 1000; //number you can randomize to your heart's content
                return new Schedule(a, TimeUnit.MILLISECONDS);
            }
        };
    }
}

你只需创建一个名为new CrawlingService的main 。开始();就是这样。

And you would simply create a main that called new CrawlingService.start(); that's it.

这篇关于如何使用Timer类调用方法,执行某些操作,重置计时器,重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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