This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Thread Safety in Web Service Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Safety in Web Service Methods" Watch "Thread Safety in Web Service Methods" New topic
Author

Thread Safety in Web Service Methods

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
I have a Web Service method with its own local variables, I understand that is thread safe since the variables are all on the stack and each thread will see its own variables. But if this same Web Service method creates via "new" another object (a utility class) to do something, wouldn't that other object created also be thread safe?

Say for example, you want to have a utility class you instantiate in the main web service method to handle some tricky string manipulations.

Since the main web service method has it's own local variables, including the one to reference the utility class instantiated, my understanding is that the utility class instantiated is by definition thread safe even if it has class attributes since it's running in the address space of the web service method that calls it.

Does this sound correct?

Thanks in advance.

-mike
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41129
    
  45
That's basically correct. The important point isn't where the object resides in memory, though, the important point is whether it is passed to some other thread. If during this calculation one of the intermediary objects somehow gets passed to another thread, problems could still arise. Admittedly, that is a somewhat contrived example in this case, because WS methods and classes are generally short lived and generally don't interact with other threads. But it illustrates the point that you still need to be careful what your code does with local variables.


Ping & DNS - my free Android networking tools app
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2273
    
  28

If you have static members in the objects that you create, they will be shared across threads. Non-static members will be thread safe.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
Ulf Dittmer wrote:That's basically correct. The important point isn't where the object resides in memory, though, the important point is whether it is passed to some other thread. If during this calculation one of the intermediary objects somehow gets passed to another thread, problems could still arise. Admittedly, that is a somewhat contrived example in this case, because WS methods and classes are generally short lived and generally don't interact with other threads. But it illustrates the point that you still need to be careful what your code does with local variables.


Thanks very much to both you both for the reply.

I'm working my way through the "Concurrency in Practice" book, but it's a bit slow going and I'm not sure with Java 7's enhancements if it's not a bit out of date?

-mike
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41129
    
  45
That's a great book - I learned concurrency "properly" from it. I haven't looked deeply into what's new with Java 7, but my understanding was it builds on top of the older stuff - so a good understanding of that would still be required (and a big step in itself :-).
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
Ulf Dittmer wrote:That's a great book - I learned concurrency "properly" from it. I haven't looked deeply into what's new with Java 7, but my understanding was it builds on top of the older stuff - so a good understanding of that would still be required (and a big step in itself :-).


Thank you for your follow up. I agree, it's an excellent book. I'm basically reading and studying the text/examples going through (currently in ch. 3) and hoping these will help ring a bell in my projects when I see issues with others' (or my) code.

-- mike
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread Safety in Web Service Methods
 
Similar Threads
static vars vs. servlet context
Synchronization
Static methods and thread safety
Are local objects thread-safe?
Thread safety question