Reputation: 11741
Here is my use case.
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.
Upvotes: 3
Views: 8964
Reputation: 618
If creating the processor (P) is expensive, can you pre-create a pool of P instances, and re-use them, in the same way that a database connection pool is created?
The Apache Commons Pool project could give you a start point.
Upvotes: 0
Reputation: 116266
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.
Upvotes: 5
Reputation: 4653
Tomcat 7 supports Servlet 3.0 and it's async servlets - it maintains own thread pool and provides standard API to execute requests in separate threads. You can see example here: Async Servlets in Tomcat 7
Upvotes: 4