Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Safety in Web Service Methods

 
Mike London
Ranch Hand
Posts: 1190
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1190
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1190
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic