This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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'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).
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??
Joined: Mar 22, 2005
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
Joined: Apr 15, 2006
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.
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.
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.
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?