• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Multi Thread

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 144
Oracle Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Greenhorn
Posts: 15
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ashok! That answered all my questions for now.
Great explanation by the way
 
Ashok Kurakula
Greenhorn
Posts: 15
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cheers
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic