aspose file tools*
The moose likes Servlets and the fly likes Servlet thread safety and effiecient design Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet thread safety and effiecient design" Watch "Servlet thread safety and effiecient design" New topic
Author

Servlet thread safety and effiecient design

Jasvir Rupana
Ranch Hand

Joined: Oct 13, 2010
Posts: 52
Hi Guys,
As we know servlet is not thread safe,so in readl time enterprise applications , what should be the architecture of servlet application which handle multiple requests without any inconsistency?can we relay on threads ??? how many maximum requests can a servlet instance handle??i.e how many maxium threads can be creatd and managed efficiently?
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
In most cases it's not hard to make servlets thread-safe by avoiding instances variables (or using synchronization if you can't) and some other techniques. So I wouldn't say "servlets are not thread-safe" - they are, if you write them properly.

If you're just starting out with Java concurrency, then you need a good book on this; the ThreadsAndSynchronizationFaq points to two excellent ones.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

As we know servlet is not thread safe,so in readl time enterprise applications , what should be the architecture of servlet application which handle multiple requests without any inconsistency?


you are right , so in servlet we will not use instance variables , but only use method level means inside doGet doPost , because each Thread which is trreated as a request will get its own copy of data .



Save India From Corruption - Anna Hazare.
Jasvir Rupana
Ranch Hand

Joined: Oct 13, 2010
Posts: 52
thanks Burnham for your quick reply .
i am aware of how to make servletrs threa safe but that is not healthy enough to make new instance per request. synchronisation is also not recomended ,we can't put request to wait in a long queue.

In my current project 40 000 user can hit at a time ,,so single thread would be fine to handle all the requests??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

"Jass Java", please check your private messages for an important administrative matter. Thanks.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

Jass Java wrote:i am aware of how to make servletrs threa safe but that is not healthy enough to make new instance per request.

Who said anything about creating new instances? A single copy of a thread-safe servlet can handle any number of simultaneous requests.

In my current project 40 000 user can hit at a time ,,so single thread would be fine to handle all the requests??

There will be one thread for each simultaneous request.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Jass Java wrote:that is not healthy enough to make new instance per request.

The cost of allocating and GC-ing objects isn't nearly what it used to be, especially with newer JVMs as found in Java 5 and Java 6. The fear of that should not drive architectural decisions.

synchronisation is also not recomended

By whom? Synchronization, too, has become way, WAY, cheaper with recent JVMs. It is, in fact, how you would guard shared mutable state.

In my current project 40 000 user can hit at a time ,,so single thread would be fine to handle all the requests??

What do you mean by "single thread"? Web apps are inherently multi-threaded. If you really have an application that needs to handle 40000 concurrent users (as opposed to 40000 potentially concurrent users) then I can almost guarantee that you will end up with multiple servers to handle that load.

But, with all due respect, it really doesn't sound like you have a good handle on the problems arising from concurrency in web apps, and what the Java facilities are that you would use to address them. You really need to read and thoroughly understand one of the eminent books on the subject.
Jasvir Rupana
Ranch Hand

Joined: Oct 13, 2010
Posts: 52
sorry i mean single instance not single thread. can we do this way to create a pool of instances to serve around 40000 requests ?? pool of 20-50 instances ?
Jasvir Rupana
Ranch Hand

Joined: Oct 13, 2010
Posts: 52
in between Bear Bibeault and Ravi thank you guys too for your replies and suggestions
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
As Bear said, there'll be a single instance of the servlet class. The servlet container will generally keep a pool of threads that handle requests, probably somewhere between dozens and hundreds of threads. If there are more simultaneous requests, they either have to wait, or are turned down.

And, as I said, you won't be handling 40000 simultaneous requests with a single server.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet thread safety and effiecient design
 
Similar Threads
about instance of servlet
thread is equal to session
One clarification needed regarding Servlet
WebServer's process management
Threads for a servlet