2

What is the difference between the following commands:

A:

    new Timer() {

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

        }
    }.schedule(1);

B:

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

        @Override
        public void execute() {
            // TODO Auto-generated method stub
        }
    });
Michael
  • 32,527
  • 49
  • 210
  • 370

3 Answers3

3

Use the Timer class to schedule work to be done in the future.

Use the Scheduler for deferring some logic into the immediate future


Scheduler says:

If you are using a timer to schedule a UI animation, use AnimationScheduler instead. The browser can optimize your animation for maximum performance.


For detailed description please have a look at DevGuideCodingBasicsDelayed.

GWT provides three classes that you can use to defer running code until a later point in time:

Timer
DeferredCommand
IncrementalCommand

Please have a look on below posts:

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
1

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. 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.

Sir Hackalot
  • 521
  • 6
  • 16
0

With your particular calls (Timer#schedule(1)) there's no functional difference. Both ways will end up calling UnloadSupport#setTimeout0 with 1 msec delay. 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. See this answer.

Community
  • 1
  • 1
Boris Brudnoy
  • 2,405
  • 18
  • 28