• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronization issues

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is below method prone to run into synchronization errors when multiple threads are executing it:


My understanding is that since str1 and str2 are primitive and being passed from the caller, and every caller's method stack is separate, there should not be any issue.
The synchronization issues will come when threads have access to same instance variables (or in case of static methods, same static variables). It could be an issue if the caller threads were passing object references to same objects. Also, there should not be any issues if we are using only method local variables (be it primitive or non-primitive).

So suppose there are 2 threads calling this method:
Thread 1: str1="Good" , str2="Morning"
Thread2: str1="Hi", str2="There"

then as per my understanding above code will never run into issue of returning "Good There" or "Hi Morning".
Could you please validate/correct my understanding on this.




 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhishk Singh wrote:. . . str1 and str2 are primitive

That is mistaken, I am afraid. Strings aren't primitives. The name of the method looks misleading, too.

. . . every caller's method stack is separate, there should not be any issue. . . .

I am not certain, but I think you are right for such utility methods. Instances can suffer synchronisation problems, but you won't have any instances of that class.
 
Abhishk Singh
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That is mistaken, I am afraid. Strings aren't primitives. The name of the method looks misleading, too.


My bad. Thanks for correcting. I wrote the method with int inputs earlier and performed add opteration on them. But to make the method look more like what my actual requirement is, changed inputs to string just before posting the question. It should have looked like below:



 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhishk Singh wrote:It could be an issue if the caller threads were passing object references to same objects.



Yes, that's correct. If two threads had references to the same object and if they changed the state of that object, that could be a problem if synchronization wasn't applied to those changes. However that isn't a problem for this code, it's a problem for the class of the objects being changed here.

But that doesn't apply to your code because it doesn't change the state of the String objects whose references are passed to it. (Of course a String object is immutable so that couldn't happen anyway, but for other types of object it could.)
 
Ranch Hand
Posts: 285
2
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhishk, If we don't bother to use append method in return, we we should not try and see with StringBuffer which is fail-safe too ?

Only a suggestion.Please add your thoughts to it if there is any impact.
 
Mohammed Sardar.
Ranch Hand
Posts: 285
2
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A small correction. *Why we should not try SB-Thread Safe?*
 
Abhishk Singh
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohammed Sardar. wrote:Abhishk, If we don't bother to use append method in return, we we should not try and see with StringBuffer which is fail-safe too ?

Only a suggestion.Please add your thoughts to it if there is any impact.



HI Mohammed, here is my understanding on the use of StringBuffer:
1. Thread safety costs some performance, so we should not use it until we really need it.
2. Rather than using "+" for concatenation of strings, StringBuffer and StringBuilder are more optimized for such operations. And among them, StringBuilder should be used until thread safety is required. So this program can be modified to use a StringBuilder which can be initialized locally in the method and can return the result after concatenation.
3. Thread safety wise, StringBuilder or StringBuffer does not matter in this method if initialized locally inside the "concatenateStrings" . But it would matter if it becomes a Static variable of the class in which static contatenateStrings method is defined.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhishk Singh wrote:. . . if it becomes a Static variable of the class in which static contatenateStrings method is defined.

I hope nobody would even think of creating such a static field. That would produce all sorts of problems other than loss of threa‍d‑safety.
Come to thin of it: would a StringBuffer lose its th‍read‑safety as a static field?
 
Abhishk Singh
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Come to thin of it: would a StringBuffer lose its th‍read‑safety as a static field?

Right. Even if its a StringBuffer, because its shared between different threads. If there are 4 statements modifying the stringBuffer object with first statement resetting it inside the method concatenateStrings and one thread loses control after executing 2nd statement and another thread starts at 1st statement and resets the buffer, we lost the modification done by thread one.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your method is accepting two immutable objects and returning an immutable object and is not modifying any instance variables, I don't see any thread synchronization issues.

Even if you use StringBuffer (which isn't thread safe) within the method in this manner it should not have any thread synchronization issues:
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are a few potential problems that I could think of:

static variable modified in static method :

instance variable modified in method :

Sharing a common mutable object that's not thread safe

 
Abhishk Singh
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clarifying Salvin.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic