• Post Reply Bookmark Topic Watch Topic
  • New Topic

why servlets are multi - threaded

 
Anant Jagania
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

We know that the servlets are multi-thraded. What is the reason behind this? Why would we need something multi-threaded which is a server side object? What is the meaning of multi-threaded application?

Thanks & Regards,
Anant
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet's are multithreaded as it much better in terms of performance as compared to multi process system ,in which in for each incoming request a new process is created to take care of that.

There are certain rules while writing servlets to make it work properly in a multithreaded environment and you have to know the life cycle of servlet to make best use of it.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that servlets are not multi-threaded by nature - it's perfectly possible to run them in single-threaded mode by having them implement "SingleThreadModel", although you should not do that.

And they're definitely not multithread-safe by nature - particularly, your code will need to be careful in using static and instance fields in a threadsafe manner.
 
satya mahapatra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1> servlets are multi-thraded. What is the reason behind this?

Ans: If servlets wont be mulithreaded then for each new request a new servlet instance will be created.Which means every time the loading, initialization process will take place. Which will take lot of time. Means performance issue. But if we will handle each request in threads then the the same process wont repeat. So performance will be better.

Q2> Why would we need something multi-threaded which is a server side object?

Ans: There is nothing like that server side object cant be multithreaded.

Q3> What is the meaning of multi-threaded application?

Ans: Just take an example of an application IExplorer. This is a single aplicatuion. But we are able to browse, download, listen music,....etc at the same time. All these are running in a separate thread. So application is only one, but there are multiple threads for diff work it is performing.

I just explained you my understanding. Corect me If i am wrong.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by satya mahapatra:
Q1> servlets are multi-thraded. What is the reason behind this?

Ans: If servlets wont be mulithreaded then for each new request a new servlet instance will be created.Which means every time the loading, initialization process will take place. Which will take lot of time. Means performance issue. But if we will handle each request in threads then the the same process wont repeat. So performance will be better.


There are various ways to handle this. The servlet container could keep a pool of initialized servlet instances around, and so avoid any init overhead at request time. Or the container could serialize the requests using a single servlet instance, so that the second request would be handled only once the first finishes.

But this is all idle speculation. These days, there's no real excuse for writing servlets in a non-thread-safe manner (unless you know, and can guarantee, that there won't be more than a single user at any one time).
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:


There are various ways to handle this. The servlet container could keep a pool of initialized servlet instances around, and so avoid any init overhead at request time. Or the container could serialize the requests using a single servlet instance, so that the second request would be handled only once the first finishes.

But this is all idle speculation. These days, there's no real excuse for writing servlets in a non-thread-safe manner (unless you know, and can guarantee, that there won't be more than a single user at any one time).


To add to that you should synchronize on the appropriate scope to make your servlets thread safe where neccessary
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!