*
The moose likes Servlets and the fly likes multithreading in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "multithreading in servlet" Watch "multithreading in servlet" New topic
Author

multithreading in servlet

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
I have done multithreading in servlet and it is working.So,does it mean that servlet will be serving request more faster because one multithreading is provided by container and other i have implemented myself.
Please clarify.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41604
    
  55
I'm not sure what you mean by "I have done multi-threading". Servlets are by nature multi-threaded - the servlet container will automatically have multiple threads run the servlet at the same time if simultaneous requests come in. The only way NOT to have multi-threading is to implement the obsolete SingleThreadModel interface (and you really should not do that).


Ping & DNS - my free Android networking tools app
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
Thanks for your reply.So it means we will be getting better performance if we are implementing multithreading in a servlet.But,people never prefer to implement multithreading in a servlet.Can you tell me why it is so??
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41604
    
  55
So it means we will be getting better performance if we are implementing multithreading in a servlet.

Again, I'm not sure what you mean by "implement multithreading". Servlets are multi-threaded, unless you take the specific step I mentioned before. The servlet developer of course has to make the servlet code thread-safe, but it's multi-threaded regardless of thread-safety.

The multi-threaded operation of servlets has nothing to do with creating Threads objects in the servlet code, in case that's what you're wondering about.

people never prefer to implement multithreading in a servlet.

I don't know where you got this idea. Multithreading is essential for web sites where performance is a factor (which means just about all of them).

But since you seem to use "implement multithreading" in an incorrect way, maybe you can make it clearer what you mean by this statement.
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
Implement multithreading means : In Post method i have done

Thread t =new Thread(this)
t.start();
and then implemented run method in the same servlet...

If this can be done in a servlet,why don't people do this in servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Originally posted by Raj Kumar Bindal:
If this can be done in a servlet,why don't people do this in servlet.

Because there is absolutely no advantage to doing so, or any reason for doing so. All that it does is to needlessly complicate things.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rajesh Vassey
Greenhorn

Joined: Apr 28, 2008
Posts: 8

I guess it would help our fellow developers who search for same question and seek answer.
Please correct me if i am wrong.
-------------------------------------------------------------------------------------------
The Servlet instance is per application which is created by Servlet Container; So if mulitple requests are coming to servlet, container itself creates a new thread for each request and allows to work on Servlet object.So we no need to create a thread in servlet code explicitly to make use of goodness of multithreading. Already it supports multithreading and so as a programmer we have to concentrate on what would happen when mulitple requests(multiple threads) are accesing your instance data of servlet so in that case concentrate on Synchronized coding concepts and make it thread safe and enjoy multithreading in servlet.

If you are not thinking of performance and criteria is only to achieve thread safe then think of your servlet implementing javax.servlet.SingleThreadModel.


--SCJP,SCWCD--
Nam Ha
Ranch Hand

Joined: Aug 05, 2011
Posts: 50

Raj Kumar Bindal wrote:I have done multithreading in servlet and it is working.So,does it mean that servlet will be serving request more faster because one multithreading is provided by container and other i have implemented myself.
Please clarify.


I don't see any reason to create a new thread to run your logic in aServlet.

Can you show some real life examples that needs to run multiple threads in a Servlet?


Java Object Creation
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3827

Welcome to CodeRanch, Rajesh Vassey!


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3827

Vivekana Gops,
Your post was moved to a new topic.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: multithreading in servlet