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

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

问题描述

我是一名 Java 初学者,一直在寻找解决这个问题的各种解决方案,让我自己有点纠结.我已经尝试过使用 Threads,然后发现了这个 Timer 类,并且到目前为止没有成功.如果您可以发布带有 main 方法的可执行代码,以便我可以看到它工作并从那里开始播放,那就太好了.

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. 当 Timer 关闭时,再次调用 doSomething().

可能使用这个: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.

此外,如果您需要安排代码的执行,请查看Guava Services 因为它确实可以让你的代码更清晰,并且抽象了很多创建线程、调度等的样板.

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.start() 的 main ;就是这样.

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

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

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