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.
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.
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.
Joined: May 14, 2004
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.