File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet thread safety and effiecient design

 
Jasvir Rupana
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .

 
Jasvir Rupana
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Jass Java", please check your private messages for an important administrative matter. Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in between Bear Bibeault and Ravi thank you guys too for your replies and suggestions
 
Lester Burnham
Rancher
Posts: 1337
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic