aspose file tools*
The moose likes Threads and Synchronization and the fly likes A threading doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "A threading doubt" Watch "A threading doubt" New topic
Author

A threading doubt

amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 512
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
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Vivek Kr Singh
Ranch Hand

Joined: Oct 12, 2007
Posts: 56
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

Joined: May 14, 2004
Posts: 512
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
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

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

Joined: Sep 19, 2008
Posts: 385
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.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A threading doubt