File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Multi Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Multi Thread" Watch "Multi Thread" New topic
Author

Multi Thread

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I am getting confused with Servlets and Multi Thread (Synchronization and Locks)

"Synchronizing the service method will stop other threads from the same Servlet from accessing the Context attributes but it won't protect the access from a completely different Servlet or JSP"

From my understanding, I thought , after Synchronizing the Service() or doGet()/doPost() method, all thread would access the service method one at a time whether it's from the same or different Servlet.

How can other Servlet have free access to it ?
Can some one explain how exactly this works? or give me a link for a better understanding of Multi thread.

Couldn't find anything which was easy to understand the working ...

Always had problem understanding Synchronization and Multi Thread



The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1678
    
  25

Hi Nabila,

"Synchronizing the service method will stop other threads from the same Servlet from accessing the Context attributes but it won't protect the access from a completely different Servlet or JSP"

The context attributes are accessible by all servlets in the web-application, so if you synchronize one servlet they can still be accessed by the other servlets (they all have their own service() method) you have defined in your web.xml.

(note: there is one exception: if you have only one web-app with only one servlet, then it is sufficient to synchronize the service() method)

Regards,
Frits
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

Suppose that you have an application scope attribute named LatestServlet.
In ServletA's code, you set LatestServlet to "A".
In ServletB's code, you set LatestServlet to "B".

Synchronizing the service methods of both Servlets ensures that only one thread of ServletA is running and one thread of ServletB is running. This does not prevent from both of those thread occuring simultaneously.

Look at the following code for SerlvetA


In line 1 you set the value to "A". In line two to put the Thread to sleep for 5 seconds. During those 5 seconds ServletB's thread could run and change the value of LatestServlet to "B". Because of that, you cannot guranatee the value of LatestServlet by the time you execute line 3.

Note that this can still happen even without the sleep statement there. It just easier to understand the concept with the sleep statement there. Also remember that this is also true for session attributes as well. If you have two tabs open within the same browser, they will share the same session. I hope this helped a little.


OCJP6, OCWCD5
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661

Thanks alot. That did make it a little clearer.

So since browsers maintain session , new window of the same browser will also share the same session . However , a different browser altogether will create a new session... Right?

(Still trying to settle it in my head !)

Could you explain (preferably with examples) how Request Attribute and Local variables are Thread Safe ?
Ashok Kurakula
Greenhorn

Joined: May 16, 2011
Posts: 15

Hi Nabila,

So since browsers maintain session , new window of the same browser will also share the same session . However , a different browser altogether will create a new session... Right?


Yes, a different browser will always create a new session.

Could you explain (preferably with examples) how Request Attribute and Local variables are Thread Safe ?


Whenever there is a new request from a client, the container creates new request and response objects and sends them to the servlet's service method. After the job is done, it destroys the request and response objects. So, it always the new request object that the servlet's service method gets. Hence at any given time, a request object is never shared by multiple threads of the same servlet or of the different servlets. Hence the request attributes are thread safe.

Local variables are always declared within the method and their scope ends with the method. So each thread has its own copy of the local variables. They can't possibly be accessed/modified by other threads. So local variables too are thread safe.


OCPJP 6, OCE Java EE 6 JSP and Servlets Developer, OCE Java EE 6 EJB Developer...
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Thanks Ashok! That answered all my questions for now.
Great explanation by the way
Ashok Kurakula
Greenhorn

Joined: May 16, 2011
Posts: 15

cheers
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Ashok Kurakula wrote:cheers


hey guys. i have to question.. service() method is threadsafe?? request and response object are thread safe??

kathy sierra says it is.. but look this at some scwcd study guide.
A servlet container may send concurrent requests through the service method of
the servlet. To handle the requests the developer of the servlet must make
adequate provisions for concurrent processing with multiple threads in the
service method
.
An alternative for the developer is to implement the SingleThreadModel interface
which requires the container to guarantee that there is only one request thread at
a time in the service method. A servlet container may satisfy this requirement by
serializing requests on a servlet, or by maintaining a pool of servlet instances.
If the servlet is part of a web application that has been marked as distributable,
the container may maintain a pool of servlet instances in each VM that the
application is distributed across.
For servlets not implementing the SingleThreadModel interface, if the service
method (or methods such as doGet or doPost which are dispatched to the
service method of the HttpServlet abstract class) has been defined with the
synchronized keyword, the servlet container cannot use the instance pool
approach, but must serialize requests through it.
It is strongly recommended that developers not synchronize the service
method (or methods despatched to it) in these circumstances because of
detrimental effects on performance.
Thread Safety
Implementations of the request and response objects are not guaranteed to be
thread-safe. This means that they should only be used within the scope of the
request handling thread.

can somebody explain best regards..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multi Thread