简体   繁体   English

GWT:计时器和调度程序类

[英]GWT: Timer and Scheduler Classes

I have read this page over several times, and am just not seeing some of the inherent differences between GWT's Timer and Scheduler classes. 我已多次阅读此页面 ,我只是没有看到GWT的TimerScheduler类之间的一些固有差异。 I'm looking for the use cases and applicability of each of the following: 我正在寻找以下各项的用例和适用性:

  • Timer , Timer::schedule and Timer::scheduleRepeating 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? 这只是GWT的方式,提供多种方式做同样的事情? If not, please help me understand when and where each is appropriately used. 如果没有,请帮助我了解每个适当使用的时间和地点。

Use Scheduler when you need a browser to complete whatever it is currently doing before you tell it to do something else. 当您需要浏览器完成其当前正在执行的任何操作之前,请使用Scheduler ,然后再告诉它执行其他操作。 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 Timer if you want some action to happen after a specified period of time. 如果您希望在指定的时间段后执行某些操作,请使用Timer For example: 例如:

 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. 此代码将显示notificationPanel,然后它将在10秒后隐藏它。

As the JavaDoc says, DeferredCommand is deprecated in favor of Scheduler . 正如JavaDoc所说, DeferredCommand已被弃用,而有利于Scheduler The problem with DeferredCommand and IncrementalCommand is that they have a static state (which makes it hard to use reliably in tests). DeferredCommandIncrementalCommand的问题在于它们具有静态(这使得在测试中难以可靠地使用)。 Moreover, their (static) methods make JSNI calls which forces you to use a GWTTestCase to test your code (static methods aren't –easily– mockable). 此外,它们的(静态)方法使JSNI调用强制您使用GWTTestCase来测试代码(静态方法不容易模拟)。 Static methods also make it impossible to wrap them (to, eg 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"). 另一方面,您使用Scheduler的实例(如果您想要可测试的代码,您将使用依赖注入来获取调度程序的实例,并且永远不会调用Scheduler.get()除非您的DI“工厂” )。 In a test, you can then use a StubScheduler for instance. 在测试中,您可以使用StubScheduler

Then there's Timer , which is similar to the others but the scheduled task can be cancelled. 然后是Timer ,它与其他类似,但可以取消计划任务。 Note that Timer makes use of JSNI too, just like DeferredCommand ; 请注意, Timer也使用JSNI,就像DeferredCommand一样; any kind of code that uses a Timer will need a GWTTestCase to be unit-tested. 任何使用Timer的代码都需要GWTTestCase进行单元测试。

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

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