Reputation: 33307
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
}
});
Upvotes: 2
Views: 1820
Reputation: 541
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.
Upvotes: 1
Reputation: 2460
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.
Upvotes: 0
Reputation: 46881
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:
Upvotes: 3