简体   繁体   English

我需要帮助理解java中的Timer类的scheduleAtFixedRate方法

[英]I need help understanding the scheduleAtFixedRate method of theTimer class in java

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 has a LOT of questions about using timers to control delays before user input and the like, but not a lot on standalone timers. Stack有很多关于在用户输入之前使用定时器来控制延迟的问题,但在独立的定时器上却没有太多问题。 I've run across this code from a friend, and have studied the class on Java Documentation. 我从朋友那里遇到了这段代码,并研究了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);

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. 乍一看,考虑到scheduleAtFixedRate(TimerTask task, long delay, long period)我可以看到delayperiod参数,但不能看到第一个参数之前的开放状态。

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? 这是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);

That code is equivalent to this refactoring, where the new TimerTask is assigned to a local variable. 该代码等效于此重构,其中new TimerTask被分配给局部变量。

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

timer.scheduleAtFixedRate(task, delay, period);

Of course the weird part has just moved upwards a bit. 当然,奇怪的部分刚刚向上移动了一点。 What is this new TimerTask stuff, exactly? 什么是这个new TimerTask东西,究竟是什么?

Java has special syntax for defining anonymous inner classes . Java具有用于定义匿名内部类的特殊语法。 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. 您可以在使用点定义它及其run()方法,而不是在别处定义TimerTask的子类。

The code above is equivalent to the following, with the anonymous TimerTask sub-class turned into an explicit named sub-class. 上面的代码等同于以下代码,匿名的TimerTask子类变成了一个显式命名的子类。

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

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

You are correct, the first parameter is the entire code block: 你是对的,第一个参数是整个代码块:

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

These declarations are called Anonymous classes and are explained in more detail in the Java Tutorials . 这些声明称为匿名类 ,在Java教程中有更详细的解释。

It is a anonymous inner class . 这是一个匿名的内部阶级 You need to study inner classes for understanding this . 你需要学习内部类来理解这一点 Generally such classes are used when you do not need the class to be used else where in your code. 通常,当您不需要在代码中的其他位置使用类时,将使用此类。 You cannot use it else where just because you dont have reference pointing to it. 你不能在其他地方使用它只是因为你没有指向它的引用。 You can also replace the above code as follows : 您也可以按如下方式替换上面的代码:

class MyTimerTask extends TimerTask {

        @Override
        public void run() {
            // Timer task code goes here.
             System.out.println(setInterval());
        }

    }
    MyTimerTask timerTask = new MyTimerTask();
    timer.scheduleAtFixedRate(timerTask, delay, period);

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

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