java GWT:计时器和调度程序类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12368316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:36:23  来源:igfitidea点击:

GWT: Timer and Scheduler Classes

javagwtscheduler

提问by IAmYourFaja

I have read this pageover several times, and am just not seeing some of the inherent differences between GWT's Timerand Schedulerclasses. I'm looking for the use cases and applicability of each of the following:

我已经多次阅读此页面,只是没有看到 GWTTimerScheduler类之间的一些内在差异。我正在寻找以下各项的用例和适用性:

  • Timer, Timer::scheduleand Timer::scheduleRepeating
  • Scheduler::scheduleDeferred
  • Scheduler::scheduleIncremental
  • IncrementalCommand
  • DeferredCommand
  • TimerTimer::scheduleTimer::scheduleRepeating
  • Scheduler::scheduleDeferred
  • Scheduler::scheduleIncremental
  • IncrementalCommand
  • DeferredCommand

These all appear to be doing the same thing, more or less, and it feels like you can accomplish the same objectives with all of them. Is this just GWT's way a providing multiple ways of doing the same thing? If not, please help me understand when and where each is appropriately used.

这些似乎或多或少都在做同样的事情,感觉就像你可以用它们来实现相同的目标。这只是 GWT 提供多种方式来做同一件事的方式吗?如果不是,请帮助我了解何时何地适当使用每种方法。

回答by Andrei Volgin

Use Schedulerwhen you need a browser to complete whatever it is currently doing before you tell it to do something else. For example:

当您需要浏览器完成当前正在执行的任何操作,然后再告诉它执行其他操作时,请使用调度程序。例如:

myDialogBox.show();
Scheduler.get().scheduleDeferred(new ScheduledCommand() {

    @Override
    public void execute() {
        myTextBox.setFocus();
    }
});

In this example, focus will not be set until the browser completes rendering of the dialog, so you tell the program to wait until the browser is ready.

在这个例子中,在浏览器完成对话框的渲染之前不会设置焦点,所以你告诉程序等待浏览器准备好。

Use Timerif you want some action to happen after a specified period of time. For example:

如果您希望某些操作在指定的时间段后发生,请使用Timer。例如:

 notificationPanel.show();
 Timer timer = new Timer() {
     @Override
     public void run() {
         notificationPanel.hide();
     }
 };
 timer.schedule(10000);

This code will show notificationPanel, and then it will hide it after 10 seconds.

此代码将显示通知面板,然后在 10 秒后将其隐藏。

回答by Thomas Broyer

As the JavaDoc says, DeferredCommandis deprecated in favor of Scheduler. The problem with DeferredCommandand IncrementalCommandis that they have a static state (which makes it hard to use reliably in tests). Moreover, their (static) methods make JSNI calls which forces you to use a GWTTestCaseto test your code (static methods aren't –easily– mockable). Static methods also make it impossible to wrap them (to, e.g. add some logging or whatever).
On the other hand, you work with an instance of a Scheduler(if you want testable code, you'll use dependency-injection to get a instance of a scheduler and will never call Scheduler.get()except in your DI "factory"). In a test, you can then use a StubSchedulerfor instance.

正如 JavaDoc 所说,DeferredCommand不推荐使用Scheduler. 这个问题DeferredCommandIncrementalCommand是,他们有一个静态的状态(这使得它很难在测试中可靠地使用)。此外,它们的(静态)方法进行 JSNI 调用,这迫使您使用 aGWTTestCase来测试您的代码(静态方法不是 - 容易 - 可模拟的)。静态方法也使得无法包装它们(例如添加一些日志记录或其他什么)。
另一方面,您使用 a 的实例Scheduler(如果您想要可测试的代码,您将使用依赖注入来获取调度程序的实例,并且Scheduler.get()除了在您的 DI“工厂”之外永远不会调用)。在测试中,您可以使用一个StubScheduler例如。

Then there's Timer, which is similar to the others but the scheduled task can be cancelled. Note that Timermakes use of JSNI too, just like DeferredCommand; any kind of code that uses a Timerwill need a GWTTestCaseto be unit-tested.

然后Timer是 ,与其他类似,但可以取消计划任务。请注意,它Timer也使用了 JSNI,就像DeferredCommand; 任何使用 a 的代码Timer都需要GWTTestCase进行单元测试。