0

I want to use string id to synchronization my code.

this is what I am trying to implement.

public void executeSomeCode(Object object)
{
   String firstString= object.getFirstString();
   String  secondString= object.getSecondString();
   
   Synchronization(firstString)
   {
      Synchronization(secondString)
      {
       //retrieve data from database and execute code here and save back them.
      }
   }

}

but can we use directly string for synchronisation as mention in above diagram.

I already implemented synchronization using concurrenthashmap as mention in below link and that's working fine. https://stackoverflow.com/a/54672371/11053922

edit :- to avoid deadlocks,i will use these String in a lexicographically order.

  • 1
    This is a dangerous approach. For one, you risk deadlock e.g. if you have two concurrent transactions where A pays B and B pays A (avoid this by acquiring the locks in an order independent of who is paying whom e.g. ordered lexicographically). For another, you're locking on locks that other parties can access, potentially preventing you from acquiring either of the locks. – Andy Turner Aug 29 '20 at 18:44
  • `will above method work always even if I am getting this object from different-different system??` I don't know what a "different-different" system is, but the answer is probably "no". Synchronization only works within a single JVM. Different JVMs running on the same system, or on different systems, are completely outside of the preview of this kind of synchronization. – markspace Aug 29 '20 at 19:03
  • @AndyTurner while I agree with the second statement in your case, I am wondering why you see a deadlock in this sequence? deadlock might get imposed if this order is reversed at some otther place, but there is no evidence of him talking about it.. Am I missing something? – Anand Vaidya Aug 29 '20 at 19:09
  • @markspace here different-different system means that I am getting this object from an api and. – kanaram bhari Aug 29 '20 at 19:10
  • @AnandVaidya AndyTumeri is right, I edited my question little bit. can you tell me that this will work or not?? – kanaram bhari Aug 29 '20 at 19:13
  • Note that you are locking those particular string instances. If another thread gets a copy of the same object with identical string values, that thread can lock those strings as well. Is that what you're trying to do, that is, synchronize processing based on the string values? – Burak Serdar Aug 29 '20 at 21:07
  • @Burakserdar I jast want here that if there Is any other thread with same string values should not interfere to each other. – kanaram bhari Aug 30 '20 at 05:56
  • What you are doing here is synchronizing using the string object, not the value of the string. If the same `object` instance is shared among threads and that instance returns the same string instances, this will work. If somehow another thread gets a different copy of `object` with same string values, it will not. – Burak Serdar Aug 30 '20 at 05:58
  • But why do you want to aquire lock on the Strings? – Anand Vaidya Aug 30 '20 at 10:04
  • https://stackoverflow.com/questions/3984987/locking-on-strings – Anand Vaidya Aug 30 '20 at 10:05

0 Answers0