aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Urgent : Thread Safe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Urgent : Thread Safe" Watch "Urgent : Thread Safe" New topic
Author

Urgent : Thread Safe

R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
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


    "Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
    Rowan Chattaway
    Ranch Hand

    Joined: Jan 18, 2002
    Posts: 75
    I think your right. I agree with all those, except the last one:
    "if var is private member then it is thead safe."


    The early bird may get the worm, but the second mouse gets the cheese.........<br /> <br />Sun Certified Programmer for Java 2 Platform<br />Sun Certified Web-Component Developer for J2EE Platform
    R K Singh
    Ranch Hand

    Joined: Oct 15, 2001
    Posts: 5371
    thanks .. but JWeb+ says something else...
    Paul Anilprem
    Enthuware Software Support
    Ranch Hand

    Joined: Sep 23, 2000
    Posts: 3285
        
        7
    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.


    Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
    Quality Guaranteed - Pass or Full Refund!
    R K Singh
    Ranch Hand

    Joined: Oct 15, 2001
    Posts: 5371
    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

    Joined: Oct 15, 2001
    Posts: 5371
    Here I have posted an errata in JWeb+ .. plz check that also ...
    Anthony Villanueva
    Ranch Hand

    Joined: Mar 22, 2002
    Posts: 1055
    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

    Joined: Oct 15, 2001
    Posts: 5371
    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
    Ranch Hand

    Joined: Sep 23, 2000
    Posts: 3285
        
        7
    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

    Joined: Oct 15, 2001
    Posts: 5371
    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
    Ranch Hand

    Joined: Sep 23, 2000
    Posts: 3285
        
        7
    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

    Joined: Oct 15, 2001
    Posts: 5371
    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
    http://www.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
    Ranch Hand

    Joined: Sep 23, 2000
    Posts: 3285
        
        7
    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

    Joined: Oct 15, 2001
    Posts: 5371
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Urgent : Thread Safe