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

Urgent : Thread Safe

 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
A variable is thread safe if
  • A variable is thread safe if variable is local variable to method
  • A variable is thread safe if var is under synchroinized block
  • A variable is thread safe if var is member varibale of servlet which implements SingleThradModel but
  • [list] if var is static and public than it will not be thraed safe.
  • if var is private member then it is thead safe.
  • [/list]
    The last point, I have doubt. If my service method creates a thread which is accessing the private member var of servlet which has implemented STM, then how it can be thread safe as spec only gaurantees that no service of same instance will execute at the same time, but other thread can execute.
    I think private insatace var are also not thread safe but JWeb+ says that it will be thread safe.
    TIA
     
    Ranch Hand
    Posts: 75
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I think your right. I agree with all those, except the last one:
    "if var is private member then it is thead safe."
     
    R K Singh
    Ranch Hand
    Posts: 5399
    1
    Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks .. but JWeb+ says something else...
     
    Enthuware Software Support
    Posts: 4896
    60
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Ravish,
    Can you please tell me which question found such a statement that says "private members are thread safe"? Because I don't think anything like that is mentioned in JWebPlus.
    -Paul.
     
    R K Singh
    Ranch Hand
    Posts: 5399
    1
    Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Paul
    here is the question
    ================
    Question ID :996777305041
    Consider the following code :

    Which of the following statements are correct?
    ================
    ============ This is Comments from JWeb+ ===========
    When a Servlet implements SingleThreadModel interface (This interface it has no methods), the servlet container makes sure that at a time only one thread is executing it's methods. If there are multiple simultaneous requests to the same servlet then it may create multiple instances of that servlet. So, 'hm' is thread safe. It would not have been threads safe if the servlet did not implement 'SingleThreadModel' interface.
    Of course, static members can't be thread safe because multiple instances will still refer to the same object.
    Request object is thread safe because for each request a new Request object is created by the container.
    ===========================================
    though here private word is not used but still it says that private member are thread safe.
    Thanks a lot Paul for your quick responses
     
    R K Singh
    Ranch Hand
    Posts: 5399
    1
    Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here I have posted an errata in JWeb+ .. plz check that also ...
     
    Ranch Hand
    Posts: 1055
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There is only one servlet instance per registered servlet. Normally, when concurrent requests are handled by the web server, threads are spawned which executes the service() method of the servlet. Instance variables are not thread safe, but local variables within the service() method are. The request object is also "localized" per thread.
    According to the Servlet specs, when a servlet implements the SingleThreadModel interface (which is an empty "tag" interface) and concurrent requests are handled by the web server, the container is supposed to create a new servlet instance per request. It is possible that the container will pool these servlet instances. it is also possible that the container will serialize, i.e. queue, the requests per instance. It is possible that the container implementation is a combination of these.
    Since there are now multiple servlet instances handling each request, the instance variables are now thread safe, whether they are private or otherwise.
    In either case, however, if any servlet static variables are defined, these will NOT be thread safe unless you explicitly make it so with custom code.
     
    R K Singh
    Ranch Hand
    Posts: 5399
    1
    Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Anthony Villanueva:
    Since there are now multiple servlet instances handling each request, the instance variables are now thread safe, whether they are private or otherwise.


    Hi Anthony
    if my doXXX() is creating a thread which is using private member then other thread of that object will get tampared value of that member var.(though only one thread is being created by container at a time for each request)
    As container does not keep account of user created threads.
    I hope that now my question is clear .
    TIA
     
    Paul Anilprem
    Enthuware Software Support
    Posts: 4896
    60
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ravish, you are taking the statement totally out of context. hm is thread safe NOT because it is private, it is thread safe because the servlet implements STM! That's what the explanation is talking abt.
     
    R K Singh
    Ranch Hand
    Posts: 5399
    1
    Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes, paul even STM is implemented ..
    if I create a threadin my doXXX() method that is manipulating private member then the next request will get tampered value value if new thread is created from the same ServletObject.
    Let me say
    request01--> ServletObject-->createNewServiceThread-->doXXX()---createsNewUserThread--->response sent(ServletObject ready to take next request)
    request02-->sameServletObject-->createNewServiceThread-->doXXX()(NOTE: previously created UserThread has changed private member's value, so here he will ge tampred value of private member)--createsNewUserThread-->responseSent,RequestOver,threadDead.
    NOTE: ServletObject has implemented SingleThreadModel. Thats why to service threads are not running concurrently.
    I hope that now I am clear...
    TIA
    [ June 27, 2002: Message edited by: Ravish Kumar ]
     
    Paul Anilprem
    Enthuware Software Support
    Posts: 4896
    60
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You are not really supposed to create threads in the service or doXXX methods This is a standard assumption. If you do that, almost nothing will be thread safe. This includes, request object, instance members (private or public, doesn't matter).
    In any case, whether an instance memeber is private or not has nothing to with it's thread safety (in this context).
     
    R K Singh
    Ranch Hand
    Posts: 5399
    1
    Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Paul Anil:
    This includes, request object, instance members (private or public, doesn't matter).


    No Paul, request & response object will be new every time so they will be thread safe.
    I got this thinking coz I read "destroy() method is called when all threads of service die, NOT when all threads created by servlet (coz container has no info abt user created thread.)
    I think private is not thread safe ....
    I dont remember wether HashMap is thread safe or NOT.
    Wish that dont get question regarding private member for thread safty.
    I remember in some thread here also they discuss the same thing .. and concluded that .. yes it is here
    https://coderanch.com/t/164176/java-Web-Component-SCWCD/certification/SingleThreadModel
    more OR less it talks the same .... no time to go through it again .....
     
    Paul Anilprem
    Enthuware Software Support
    Posts: 4896
    60
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ravish, you are again taking statements out of context. You can access the same request/response objects if you create threads in the service method, which is what you proposed to do in your previous post. And in this case, they will not be thread safe.
     
    R K Singh
    Ranch Hand
    Posts: 5399
    1
    Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Paul Anil:
    Ravish, you are again taking statements out of context. You can access the same request/response objects if you create threads in the service method, which is what you proposed to do in your previous post. And in this case, they will not be thread safe.


    Hi Paul
    I am back ..
    AW still I am busy but still ..
    I am raising this point if creating thread coz I read this in explanation of some prob in JWeb.
    So we can say that STM does not make servlet Thread safe and even private instance var are also not thread safe in STM.
     
    The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic