This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic