SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Non-Threadsafe code

    Hi,guys! I find multi-threading one of the most difficult aspects of programming. Even with Java's simple, well-defined threading model it's all too easy to write code which LOOKS correct but is not actually thread safe.Is there any tools to avoid non-thread safe code?Can anyone tell me that why the code here is considered as not thread safe?

    Code Java:
    <%! private int idNum = 0; %>
    <%
    String userID = "userID" + idNum;
    out.println("Your ID is " + userID + ".");
    idNum = idNum + 1;
    %>

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devil_vin View Post
    Hi,guys! I find multi-threading one of the most difficult aspects of programming. Even with Java's simple, well-defined threading model it's all too easy to write code which LOOKS correct but is not actually thread safe.Is there any tools to avoid non-thread safe code?Can anyone tell me that why the code here is considered as not thread safe?

    Code Java:
    <%! private int idNum = 0; %>
    <%
    String userID = "userID" + idNum;
    out.println("Your ID is " + userID + ".");
    idNum = idNum + 1;
    %>
    I would highly recommend you to read this article, its dated back to '04 but its really good.
    http://www.javaworld.com/javaworld/j...fe.html?page=1. You wont find it too difficult with this article.

  3. #3
    SitePoint Addict ruby-lang's Avatar
    Join Date
    Aug 2007
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Remember your JSPs will be compiled into Servlets. That means your servlet will have a "idNum" attribute, and will in fact be strikingly similar to the servlet shown in page 2 of the article linked above.

    Spin anything with state out of your JSPs. It's the only way to be sure.

  4. #4
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well.A good article to clear my doubt on thread-safety.Thank a lot...

  5. #5
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,401
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, you can by synchornizing the entire JSP...lol

    well, as long as that attribute doesn't change then it's safe too

  6. #6
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,401
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    oh, back to your question. It's because there's a possibility of race condition.

    let say 2 http request has been called
    first http request = a
    second http request = b
    1) "a" is called and sets a variable x=1 from x = 7.
    2) "a" is still processing...
    3) "b" is called sets the variable "x" back to 7
    4) finally "a" needs to read "x" and it's value is "7". So, it gets the wrong value even though it has set to "1" before.

    See why now?

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2007
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to make sure I totally get this, if you were to take the ! out and just have <% int x = 1; %>, the thread safe issues go away b/c x becomes a local variable rather than an instance variable, correct?

  8. #8
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,401
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fsa3 View Post
    Just to make sure I totally get this, if you were to take the ! out and just have <% int x = 1; %>, the thread safe issues go away b/c x becomes a local variable rather than an instance variable, correct?
    Yes, that is correct. Still, unless you are doing this for school project, I highly recommend that you use servlet instead of JSP.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •