Tintin
Tintin

Reputation: 2973

Are jackson web servlets thread safe?

I am wondering how, in concept, the web requests different from threads that I can create in application itself? Do they suffer from same problems as threads do (locking, have to make state-objects thread - safe)

Are various HTTPRequests in any java - jackson (rest apis) application synchronized by web-server automatically? Are these requests not like threads and suffer from the possibility of interfering with states of various classes which are handling these web-requests? Or, is this something that application developers have to manage themselves? i.e. they do need to either synchronize or make these jackson servlets thread-safe otherwise?

Thanks

Upvotes: 0

Views: 82

Answers (2)

ebret
ebret

Reputation: 63

see : How are Threads allocated to handle Servlet request?

It is not specify in Servlet JSR, but usually a J2EE web server use one thread to handle a user request.

So many request are using one instance of your Servlet => many thread are using one instance of your Servlet.

To avoid deadlock or performance issue don't synchronized Servlet code, make them threadsafe and stateless:

See: How do servlets work? Instantiation, sessions, shared variables and multithreading

JSR Servlet spec 3.0 http://download.oracle.com/otndocs/jcp/servlet-3.0-fr-eval-oth-JSpec/

Request Handling Methods The basic Servlet interface defines a service method for handling client requests. This method is called for each request that the servlet container routes to an instance of a servlet. The handling of concurrent requests to a Web application generally requires that the Web Developer design servlets that can deal with multiple threads executing within the service method at a particular time. Generally the Web container handles concurrent requests to the same servlet by concurrent execution of the service method on different threads.

Upvotes: 1

duffymo
duffymo

Reputation: 308733

Servlets should be written as if they were single threaded. Each HTTP request coming in will be handled in a separate thread.

This means no shared mutable state in servlet classes.

If your servlet has no private data members and all operations are performed on parameters that are passed in or local objects, you're thread safe. No synchronization needed.

Shared state has to be thread safe.

Upvotes: 1

Related Questions