• 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

thread safe method calls

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

If I have a class that is accessed by multiple threads (e.g. Spring bean with singleton scope used in a web application) can I chain methods safely without synchronizing. That is, if I have method: public String getStuff(), and that method calls on several private helper methods on the same class, do I have to synchronize on the private helpers? These helper methods will simply return values to the getStuff() method, they will not access any class level variables.

Thanks in advance for your answers.
Peter
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pete Kleczka wrote:
If I have a class that is accessed by multiple threads (e.g. Spring bean with singleton scope used in a web application) can I chain methods safely without synchronizing. That is, if I have method: public String getStuff(), and that method calls on several private helper methods on the same class, do I have to synchronize on the private helpers?



If all the calls to those private methods comes from code that's already synced, then no, you don't have to. Once you obtain the lock, you keep it until you leave the sync block or call wait().

If, however, you also call those private methods from an unsynced method or block, then, yes, you have to sync those private methods.

 
Greenhorn
Posts: 14
Eclipse IDE VI Editor BSD
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If, however, you also call those private methods from an unsynced method or block, then, yes, you have to sync those private methods.



As long as those private method don't access any fields i.e. class level variables it's not necessary to sync those private methods.


If I have a class that is accessed by multiple threads (e.g. Spring bean with singleton scope used in a web application) can I chain methods safely without synchronizing.


The other question wouldn't it be better to use a request scope bean so that there is no need to synchronize the method(s).

andi
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andi Eska wrote:

If, however, you also call those private methods from an unsynced method or block, then, yes, you have to sync those private methods.



As long as those private method don't access any fields i.e. class level variables it's not necessary to sync those private methods.



Oops. Didn't notice that part of the OP.
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic