• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A threading doubt

 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have this particular scenario,
There is a multi-threaded class (Let's say a Struts Action class) and it wants to execute a static method in a Utility class.
In none of the methods ( i.e in Action class or in a Static method in utility class) , I am relying on instance/static variables.
Would it pose any problem in multi-threaded environment. I understand that my "execute" method in Action class would not pose any problems. But I am not able to somehow figure out what would be the consequences of the call to static utility method.

Regards,
Amit
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But I am not able to somehow figure out what would be the consequences of the call to static utility method.



There isn't enough detail here. There is nothing particular about static methods that makes it more thread safe or less thread safe. As with any other type of method, it would depends on what it is doing -- and how it's implemented.

Henry
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

amit punekar wrote:In none of the methods ( i.e in Action class or in a Static method in utility class) , I am relying on instance/static variables.



Your methods are not changing state of any object then they are thread safe. Depends on what your utility method does.
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Thanks for your replies.
I did not give particular details because I was trying to explain a usual J2EE scenario (using Analogy to Struts action class and utility class).
It is true that unless and until I am using any shared object that is being changed in Utility Static method it wont give rise to threading concerns.
Let's say that the utility class has "getConnection" method that returns me a JDBC connection using all method scoped code. It does not depend on anything other than parameters passed to method and local variables. Would it cause any issue in any way in multi-threaded scenario.
My understanding was
1) Action's execute method is executing in a particular thread (T1).
2) It calls Utility.getConnection() method
3) Some other thread(T2) gets the CPU and Utility.getConnection() is put aside.
4) T2 also has a call to method "Utility.getConnection()".
5) Now because the getConnection method is static, would it affect the part of the code earlier executed on account of Thread T1. ???
I have tried explaining my understanding and the doubt that I have above. Hope this helps to clarify my doubt.

Regads,
Amit

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

amit punekar wrote:5) Now because the getConnection method is static, would it affect the part of the code earlier executed on account of Thread T1. ???



As Henry already said, it doesn't make any difference whether a method is static or not when you're talking about thread safety.
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Local variables doesnot influence anything in thread as each thread will have its own copy of the local variable.

You should be very careful about implementing a method only if it uses a shared resources (i.e if it uses any class variable or instance variable that act as shared resources.)

So it all depends on how you implement the method.

Hope it helps.

Correct me if i am wrong.
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic