scheduler and timer in gwt

Comparison

Use Scheduler when 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 Timer if you want some action to happen after a specified period of time. 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.

 

Further explanation on scheduler:

JavaScript (in a browser) is single threaded. The event loop model means, we’re always in exactly one of two states:

  • in the event loop
  • executing an event handler

There are many kinds of events: Click events, onload events, XHR events, timer events, … You’ll have to declare some handlers (at least one during page load), otherwise none of your code will ever be executed. One of them is the handler you specify by implementing onModuleLoad.

It’s important to keep all handlers short, because there’s no parallelism and no interrupts (except for the last resort “unresponsive script” interrupt). This means, that users can’t interact with the interface, until the browser returns to the event loop – and that doesn’t happen before the current handler is finished.

So if you want to defer some code until after the other event handlers had a chance, then you can useScheduler.scheduleDeferred.

Scheduler.scheduleIncremental helps you to split really long running tasks into multiple steps, giving the other event handlers a chance between each of the steps.

Scheduler.scheduleFinally just means: After handling our current handler (even if an exception occurs), but before returning to the event loop, execute my command.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s