简体   繁体   English

GWT中的Scheduler和Timer有什么区别?

[英]What is the difference between Scheduler and Timer in GWT?

What is the difference between the following commands: 以下命令之间有什么区别:

A: A:

    new Timer() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

        }
    }.schedule(1);

B: B:

    Scheduler.get().scheduleDeferred(new ScheduledCommand() {

        @Override
        public void execute() {
            // TODO Auto-generated method stub
        }
    });

Use the Timer class to schedule work to be done in the future. 使用Timer类可以schedule work将来要做的schedule work

Use the Scheduler for deferring some logic into the immediate future 使用调度程序 deferring some logic到不久的将来


Scheduler says: 调度程序说:

If you are using a timer to schedule a UI animation, use AnimationScheduler instead. 如果您使用计时器安排UI动画,请改用AnimationScheduler The browser can optimize your animation for maximum performance . 浏览器可以优化动画以实现最佳性能


For detailed description please have a look at DevGuideCodingBasicsDelayed . 有关详细说明,请查看DevGuideCodingBasicsDelayed

GWT provides three classes that you can use to defer running code until a later point in time : GWT提供了三个类,您可以使用它们将正在运行的代码推迟到以后的某个时间点

Timer
DeferredCommand
IncrementalCommand

Please have a look on below posts: 请看下面的帖子:

Once scheduled via Scheduler a command cannot be canceled before it executes. 通过调度程序调度后,命令将无法在执行前取消。 You cannot remove it either. 您也无法删除它。 It will sit there and keep all of its resources until it is executed and automatically removed by Scheduler. 它会坐在那里并保留其所有资源,直到它被调度程序执行并自动删除。

This is probably not a problem, if you use Scheduler as suggested by Braj to defer logic into the immediate future. 如果您按照Braj的建议使用Scheduler将逻辑推迟到不久的将来,那么这可能不是问题。 However, it can be problematic, if you do something like this: 但是,如果您执行以下操作,则可能会出现问题:

Scheduler.get().scheduleFixedPeriod(
    myCommandUsingLotsOfResources, 
    SOME_QUITE_LONG_INTERVAL);

A timer you can cancel and throw away immediately. 您可以取消并立即丢弃的计时器。

With your particular calls ( Timer#schedule(1) ) there's no functional difference. 对于您的特定调用Timer#schedule(1) ),没有功能上的区别。 Both ways will end up calling UnloadSupport#setTimeout0 with 1 msec delay. 两种方法最终都会以1毫秒的延迟调用UnloadSupport#setTimeout0 There are differences in services Timer and Scheduler provide, however: Timer is cancellable while scheduled commands aren't; 计时器和计划程序提供的服务有所不同,但是:计时器是可取消的,而调度的命令则不可; Scheduler is more conducive to unit testing. Scheduler更有利于单元测试。 See this answer . 看到这个答案

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

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