Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A threading doubt

 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Vivek Kr Singh
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • 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
  • 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

 
Paul Clapham
Sheriff
Pie
Posts: 20756
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic