Soumya Simanta
Soumya Simanta

Reputation: 11741

Correct way of doing multithreading inside Tomcat/Jetty

Here is my use case.

  1. Client sends a request to server.
  2. 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.
  3. 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.

Upvotes: 3

Views: 8964

Answers (3)

Pidster
Pidster

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

Péter Török
Péter Török

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

Konstantin V. Salikhov
Konstantin V. Salikhov

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

Related Questions