File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Singleton methods and thread safety Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Singleton methods and thread safety" Watch "Singleton methods and thread safety" New topic
Author

Singleton methods and thread safety

Jef Smith
Greenhorn

Joined: Jul 28, 2011
Posts: 14
Just when I think I understand treads and thread safety I'm presented with a scenario that I'm not sure of. I'm hoping someone can explain the following situation with respect to thread safety.

I'm working on a legacy client-server system. There are a couple of dozen different request types and each is represented by a singleton 'command class' managed by Spring. Each command class has a single method:. For each request received a controller obtains the proper command class from Spring and calls its handleRequest() method. Each of the command classes is coded to extract its' expected parameters from the request in the handleRequest() method.

I thought at first this was not thread safe but I was told that since the request handler classes do not store the parameters in instance variables that there are no thread safety issues. The request handlers do store the parameter values in the handleRequest() method, for example:



This must be thread safe because the system has been around for a long time with no known threading issues but I'm not sure why.
Could someone explain why the variables in the singleton method are thread safe while instance variables in the same singleton would definitely not be thread safe.

Thanks,
jefs
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jef Smith wrote:There are a couple of dozen different request types and each is represented by a singleton


Just so there's no confusion: The fact the a given class is or is not a singleton has no direct bearing on its thread safety or lack thereof.

I was told that since the request handler classes do not store the parameters in instance variables that there are no thread safety issues.


It's not that simple. If there are instance variables, then there will almost certainly be thread safety issues. Removing them doesn't guarantee thread safety; it just eliminates one path of likely thread unsafety. It's still possible that those methods access objects that are shared by other threads. You need to look at that, look at what that code is actually doing.

This must be thread safe because the system has been around for a long time with no known threading issues


Alligators must not be dangerous because I've never been bitten by one yet.

Multithreading problems can lay dormant for a long time, and them manifest in subtle and unpredictable ways. The next OS patch may result in slightly different scheduling or I/O handling, which can expose a race condition that hasn't come to light yet. Or a slightly heavier load can alter the timing. Or any of a bazillion other things. And then one non-atomic x++ and a count is off by one, and something comes out just a little bit off. Reload the page a hundred times and it's find every time but that first one.

Could someone explain why the variables in the singleton method are thread safe while instance variables in the same singleton would definitely not be thread safe.


Local variables are not shared. Every method entry--whether sequential or recursive or multithreaded--gets its own copy of all those variables. So at least we know that those variables themselves are thread-safe. However, if those variables end up referring to shared objects, then we potentially have safety issues again.

Jef Smith
Greenhorn

Joined: Jul 28, 2011
Posts: 14
Jeff,

Thanks for the thorough reply. As always with thread issues I am continually reminded that I don't know the whole story.

jefs
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jef Smith wrote:Jeff,

Thanks for the thorough reply. As always with thread issues I am continually reminded that I don't know the whole story.

jefs


You're very welcome.

Multithreading is definitely an area where it's easy to get lost in the weeds. It's really made up of just a handful of simple, basic concepts, constructs, and rules, but those building blocks can combine to produce complex and counter-intuitive behavior.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton methods and thread safety
 
Similar Threads
Webapps thread basic questions
Action class in Struts
threads and static methods
setProperty and thread safety
Slap me silly, but...