• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

multithreading in servlet

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 8
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 50
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Rajesh Vassey!
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivekana Gops,
Your post was moved to a new topic.
 
reply
    Bookmark Topic Watch Topic
  • New Topic