Threading in GWT (Client)

JavaScript doesn’t support multithreading. However, GWT has a class to ‘simulate’ threading, which is not real multithreading, but in most cases does what you need: com.google.gwt.core.client.Scheduler.ScheduledCommand. The technique is based on the timer class, which executes a method after the given time elapses.

For example, when placing the following code in you own code, the scheduleDeferred method will return directly and your code continues after the command, while the execute() method is executed using the timer:

Scheduler.get().scheduleDeferred(new ScheduledCommand() {
   public void execute() {
      .. code here is executed using the timer technique.
   }
});

You can create a repeating command RepeatingCommand, which can be used to run the command more than once. Start it with Scheduler.get().scheduleIncremental() that will execute the command until the execute method returns false. You can use this to split tasks into sub tasks to get better ‘threading’ behavior. The Scheduler supports some additional methods to start a scheduled command differently. See the JavaDoc for more details.

Edited and updated with new GWT class instead of the deprecated DeferredCommand.

Leave a Comment