permaculture playing cards*
The moose likes Threads and Synchronization and the fly likes thread safe method calls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "thread safe method calls" Watch "thread safe method calls" New topic
Author

thread safe method calls

Pete Kleczka
Greenhorn

Joined: Nov 12, 2010
Posts: 5
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
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.

Andi Eska
Greenhorn

Joined: Jun 20, 2003
Posts: 14


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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
wood burning stoves
 
subject: thread safe method calls
 
Similar Threads
Strategy Pattern With Generics
Dynamic method invocation
Calling member object method using static method.
Design suggestion.
criteriaFind().. implementation