Does this case need to be thread safe in Java ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gSingh16
    New Member
    • Apr 2014
    • 1

    Does this case need to be thread safe in Java ?

    i have create a functionality such that there are two boxes left and right. The values will be checked in the left box and after clicking on right arrow (>>) the value will get moved into the Right box. Vice versa with left arrow (<<).

    I am displaying these values using Java HashMap . My question is if two different users login the site and they move the values from one box to another , ( they are seeing the same values ) than this type of scenerios need to be thread safe ( synchronized ) ? Or it will work without synchronized also ?

    By every click on arrow i am calling my java class by jquery get request and updating the Java Map in java class and getting back the response via jquery and displaying the updated values in both the boxes again.
    Last edited by Niheel; Apr 7 '14, 02:47 PM.
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    If the HashMap is shared between the users then yes, it should be synchronized two instances may be trying to access the HashMap at the same time, causing unforeseen behavior.

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      There is no way that you can synchronize two browser windows.
      Even if you synchronize the map on the server, you cannot get rid of (or synchronize) the delay between the server sending the update and the client browser showing the update.

      Better to use locking mechanism here: When the first user opens the page, he can modify entries in the boxes, but for all other users calling this page the boxes are shown, but locked until the first user finishes (or times out).

      Fazit:
      If you use locking mechanism on browser level (as a type of synchronization ), then you don't need to synchronize on java level (the HashMap)

      Please take in account that if 2 user open the same page, the first user can send his inquiry first, but the inquiry from the second user can arrive earlier at the server (different routing), so don't do anything client-browser-time based. Always use server time.

      What I mean is demonstrated in this example:
      1.) First user opens page, he can edit for 60 seconds.
      2.) second user opens page, it is locked for 60 second.
      3.) first user moves entry after 59 seconds. This query 1 is on its way to the server and will take 5 seconds.
      4.) Because 60 seconds passed (javascript browser trigger), the page of second user is unlocked. He moves the same entry immediately (after 2 seconds). This query 2 is on its way and will take 1 second.
      5.) Query 2 arrives before query 1 and will be processed successfully.
      6.) Query 1 will be processed and user 1 gets an error, because the entry to be moved cannot be found in first table.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Well, that depends on what the intention is. If you want to optimize for several people being able to modify at the same time you will have to have some kind of conflict handling. Take the conflict management in SVN as an example here - if you try to commit changes and someone has already changed it since you last updated your version there will be a merge conflict which must be resolved.
        If however you want two users to be shown the same stuff at all time, you can use a locking mechanism as described by chaarmann.

        Comment

        Working...