Threading in GWT (Client)
Solution 1
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
.
Solution 2
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.
Solution 3
New way is to use a Scheduler
Related videos on Youtube
Federer
Updated on April 11, 2020Comments
-
Federer about 4 years
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?
-
Federer about 14 yearsCorrect 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.
-
Hilbrand Bouwkamp about 14 yearsMost 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. -
SyntaxT3rr0r about 14 yearswhat 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!?
-
Hilbrand Bouwkamp about 14 years@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.
-
Ajax almost 12 yearsYou can make GWT support java.lang.Thread by super-sourcing your own custom implementation of Thread.java that can mimic whatever behavior you want in GWT only. When you call Thread.start(), server will run concurrently, and client can use Scheduler.get().scheduleRepeatingCommand / whatever you like. To write threadsafe code that can be shared with gwt, you will want to super-source a number of classes, like ThreadLocal and ConcurrentHashMap at least.