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

How can MultiThreading is implemented in Servlets

Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Hello All,

We know that either by extending Thread or by implementing Runnable, we can create a thread. But in Servlet API, we do not have any such class which extends Thread or implementing Runnable, atleast the classes HttpServlet is extending. I assume that , container would create a new thread for every request and that thread would use a single instance of Servlet. But does this mean that Servlet is designed using Singleton pattern?

Please let me know, how multithreading is implemented in Servlets.

Thanks
Ravi C.Kota


Thanks & Regards, SK
SCJP 5.0, DB2 - 800, DB2 - 803, SCDJWS (On the way)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
I assume that , container would create a new thread for every request

That is correct.

... and that thread would use a single instance of Servlet.

No. Servlets are multi-threaded, so all threads use the same servlet instance. That's why it is important to write servlets in a thread-safe manner (e.g., by not using instance variables that can change over time).

But does this mean that Servlet is designed using Singleton pattern?

That pattern is overused, IMO, and doesn't really help understanding what's going on here. (For completeness' sake it should be noted that servlet containers may instantiate more than one instance under certain circumstances, so it isn't strictly true anyway.)


Ping & DNS - updated with new look and Ping home screen widget
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Thank you Ulf.
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Hi Ulf,

I have an extension of my query. We know that while creating a thread, we either pass a thread or runnable object as a parameter or a zero arguement constructor. But my question is how does container link a newly created thread with the one time created Servlet object. I have this question, because I'm thinking how could thread access the servlet object, as servlet neither extends thread nor implements runnable. I know that my question is a bit vague,but could not express it more than this.

Thanks
Ravi C.Kota
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
A class that implements Runnable can have references to any objects you pass into it, either via the constructor or via setter method. The run method then uses those instance variables as it sees fit. No problem at all :-)
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Thank you. This clears my doubt.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can MultiThreading is implemented in Servlets
 
Similar Threads
how thread are created by the container
thread
Why Thread class is present
multithreading in servlet
extend or implement?