views:

454

answers:

3

From what I understand, the entire client side of a GWT application is converted to Javascript when you build, therefore I suppose this question is related to both Javascript and the possibilities that GWT offers.

I have a couple of dozen processes that will need to be initiated in my GWT application, each process will then continuously make calls to a server. Does GWT support threading? Does the GWT client side support threading?

EDIT:

This link states:

No JavaScript knowledge required If you’re just a user of the framework, 
which I am for the matter of discussion, you do not need to know JavaScript 
in order to write dynamic content, be it client-side such as rolling frames, 
docking panels or scheduled “multi-threading” tasks, or server-side calls 
using XMLHttpRequests (aka AJAX). 

or scheduled “multi-threading” tasks, what does this mean?

A: 

JavaScript doesn't support multithreading, so whatever GWT does, multithreading has to be done solely on the server side, because GWT can only use features that are already available on the client side.

ammoQ
Please see update.
day_trader
+1  A: 

There is work on Web Workers as part of HTML5 that is implemented in a number of browsers, but not on all (most notably internet explorer). You could use these features where available, but what you should do is look at the javascript programming model.

Javascript generally works asynchronously. Requests are fired off and at some point their answers are received as an event. You can have a large number of pending requests at the same time. This will require a bit of a redesign of your system though.

Paul de Vrieze
+6  A: 

JavaScript doesn't support multithreading. However, GWT has a class to get something, which is not real multithreading, but in most cases does what you need: com.google.gwt.user.client.DeferredCommand. 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 addCommand, will return directly and you code continues after the command, while the execute() method is executed using a the timer "trick":

DeferredCommand.addCommand(new Command() {
   public void execute() {
      .. code here is executed using the timer technique.
   }
});

You can also use the IncrementalCommand instead of Command, which can be used to run the command more than once, that is until the execute method returns false. You can use this to split tasks into sub tasks to get better threading behavior. See the JavaDoc of the classes mentioned here for more details.

Hilbrand
Correct me if I'm wrong but - I can use Java threads on the Server side, correct? If this is correct, then I think your answer has helped me a great deal, because I think I might be able to redesign my system around DeferredCommand on the client side and Java Threads on the server side.
day_trader
Most likely you can't use threads in the server itself, because the server disallows creating threads. But each call to the server from the browser will initiate a `thread`, and a thread is most likely only needed if you want to start a process that is not related to a call from the browser or you don't want the call from the browser to wait for the answer from the server. But in general it all depends on what you are trying to do with your application.
Hilbrand
@Hilbrand: what do you mean by *"the server disallows creating threads"*? Which server? I'm not saying it's good practice but if Tomcat hasn't security policies explicitly preventing thread creation you can create thread on the server side like in any Java program no!?
Webinator
@WizardOfOdds: I should have been more specific. It's not allowed when security policies disallow it just as you mention and not in an EJB, but the latter doesn't apply to Tomcat. Anyway, I would not recommend using threads in a server.
Hilbrand