Here is my use case.
- Client sends a request to server.
- The server need to do the following
2.a. Make a network call to get some data (D).
2.b. Create a processor (P) to process the data. - P processes D and sends the response back to client
Creating the processor is expensive (around the order of 1-3 seconds.)
But it doesn’t depend on the data D.
My plan is to execute the network call and creation of the processor in parallel using two different threads.
I’ve never done multithread programming inside an app server. My question is what is the best way to deal with threads inside an app server (specifically Tomcat and Jetty)
Thanks.
IMO your best bet is to use the Executor framework. This makes dealing with concurrency much easier.
Here are a couple of tutorials to get you started.
The fact that your code is running inside a web container such as Tomcat should not bother you too much. It means that the actual thread handling the request is in fact a worker thread, taken from a thread pool managed by the application server itself. However, as long as your own threads do their job cleanly (i.e. only modify data confined to this actual request and don’t interfere with other, external threads), and there aren’t too many of them at the same time, everything should be fine.