我需要帮助理解java中的Timer类的scheduleAtFixedRate方法 [英] I need help understanding the scheduleAtFixedRate method of theTimer class in java

查看:259
本文介绍了我需要帮助理解java中的Timer类的scheduleAtFixedRate方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

成为番茄工作技术的粉丝我正在制作一个倒数计时器让我完成任务我的作业。然而,这个特殊的项目不是功课。 :)

Being a fan of the Pomodoro technique I'm making myself a countdown timer to keep me on task with my homework. This particular project, however, is NOT homework. :)

Stack有很多关于在用户输入之前使用定时器控制延迟等问题,但在独立定时器上却没有太多问题。我从朋友那里学过这段代码,并且已经学习了Java文档课程。

Stack has a LOT of questions about using timers to control delays before user input and the like, but not a lot on standalone timers. I've run across this code from a friend, and have studied the class on Java Documentation.

public class Stopwatch {
    static int interval;
    static Timer timer;

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Input seconds => : ");
        String secs = sc.nextLine();
        int delay = 1000;
        int period = 1000;
        timer = new Timer();
        interval = Integer.parseInt( secs );
        System.out.println(secs);
        timer.scheduleAtFixedRate(new TimerTask()
        {
            public void run()
            {
                System.out.println(setInterval());
            }
        }, delay, period);
    }
    private static final int setInterval()
    {
        if( interval== 1) timer.cancel();
        return --interval;
    }
}

有些语法对我来说并不清楚。考虑:

There is some syntax that's not clear to me. Consider:

timer.scheduleAtFixedRate(new TimerTask()
{
     public void run()
     {
          System.out.println(setInterval());
     }
}, delay, period);

我不明白括号和括号是如何工作的。乍一看,考虑到使用 scheduleAtFixedRate(TimerTask任务,长延迟,长时间)我可以看到延迟期间参数,但不是第一个参数之前的开放式参数。

I'm not understanding how the parentheses and braces work. At first glance, given the usage of scheduleAtFixedRate(TimerTask task, long delay, long period) I can see the delay and period parameters, but not an open paren preceding first parameter.

我的第一个参数实际上是整个代码块吗?我希望整个区块被圆括号包围......但事实并非如此。这是java中的常见语法吗?我以前从未碰过它。

Is my first parameter actually this whole block of code? I would expect the whole block to be surrounded by parentheses...but it's not. Is this a common syntax in java? I've never run across it before.

new TimerTask()
{
public void run()
{
System.out.println(setInterval( ));
}
}

new TimerTask() { public void run() { System.out.println(setInterval()); } }

我只是想在我开始改变之前澄清一下我的理解。

I just want to clarify that I understand it before I start mucking about with changes.

推荐答案

timer.scheduleAtFixedRate(new TimerTask()
{
     public void run()
     {
          System.out.println(setInterval());
     }
}, delay, period);

该代码相当于此重构,其中 new TimerTask 被分配给一个局部变量。

That code is equivalent to this refactoring, where the new TimerTask is assigned to a local variable.

TimerTask task = new TimerTask()
{
     public void run()
     {
          System.out.println(setInterval());
     }
};

timer.scheduleAtFixedRate(task, delay, period);

当然奇怪的部分刚刚向上移动了一点。什么是新的TimerTask 这些东西,确切地说?

Of course the weird part has just moved upwards a bit. What is this new TimerTask stuff, exactly?

Java有特殊的语法来定义匿名内部类。匿名类是语法上的便利。而不是在其他地方定义 TimerTask 的子类,您可以在此处定义它及其 run()方法使用。

Java has special syntax for defining anonymous inner classes. Anonymous classes are a syntactical convenience. Instead of defining a sub-class of TimerTask elsewhere you can define it and its run() method right at the point of usage.

上面的代码等同于以下代码,匿名 TimerTask 子类变成了显式命名子类。

The code above is equivalent to the following, with the anonymous TimerTask sub-class turned into an explicit named sub-class.

class MyTimerTask extends TimerTask
{
     public void run()
     {
          System.out.println(setInterval());
     }
}

TimerTask task = new MyTimerTask();
timer.scheduleAtFixedRate(task, delay, period);

这篇关于我需要帮助理解java中的Timer类的scheduleAtFixedRate方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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