wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Is this method thread safe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Is this method thread safe?" Watch "Is this method thread safe?" New topic
Author

Is this method thread safe?

rajaraman navaneethan
Ranch Hand

Joined: Feb 26, 2005
Posts: 86
Hi,
I have a simple question about threads.

int methodA(int k)
{
k++;
return k;
}


Is the above method thread safe?or is it necessary to add synchronized keyword to make itthread safe?I have a small confusion as this method deals with method variables only and not any instance variable.Please clarify.

regards,
Raja
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi.

When methodA is called, a copy of k is passed as argument. The method works only in this copy. Therefore, in this case there's no need to deal with tread-safety issues.
[ June 04, 2007: Message edited by: Leandro Melo ]

Leandro Melo
SCJP 1.4, SCWCD 1.4
rajaraman navaneethan
Ranch Hand

Joined: Feb 26, 2005
Posts: 86
Hi Leandro Melo,
Thanks for the quick reply.But I have a doubt.Supposing if there are two threads which are accessing the same method at the same time, and one thread passes value 10 for k, and another thread 20 for k, how will the method be thread safe?sorry if my question is too basic,I do not have real time experience in threads.

regards,
Raja
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi rajaraman.

Thread-safety is associated with the concept of global or somehow shared data. Regarding your question specifically, there's an extra thing to discuss about , which is the fact you're passing a built-in type (an int). Java passes by value. Therefore, when thread1 passes 10 and thread2 passes 20, actually, copies of those values are being incremented. So, unless you assign the return value of the method to the original k, it's not modified. Check out this code:



Therefore, it doesn't make much sense to talk about threa-safety in this situation. Now, suppose methodA doesn't have any parameters. It's just a method that modifies an internal variable (suppose k) of class Test. Then, it might be the case where methodA needs to be synchronized. Because, others threads (or classes) might share the same k in the same instance of class Test.

Another situation would be for example, if methodA received an object as its argument. And this object is an HTTP session object shared by your application (or maybe, a member of a Singleton). Again, this is a typical situation where you would need to synchronize access to this specific object.

Is it clear now?
rajaraman navaneethan
Ranch Hand

Joined: Feb 26, 2005
Posts: 86
Hi Leandro Melo,
Thanks for the nice reply with the example.The concept is clear now.

regards,
Raja
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by rajaraman navaneethan:
But I have a doubt.Supposing if there are two threads which are accessing the same method at the same time, and one thread passes value 10 for k, and another thread 20 for k, how will the method be thread safe?


Local variables and method parameters are stored on the stack. Each thread has its own stack, and each method call gets its own space on the stack of the current thread (the latter is why recursion actually works).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
rajaraman navaneethan
Ranch Hand

Joined: Feb 26, 2005
Posts: 86
Hi Ilja Preuss,
Thanks for the explanation.

regards,
Raja
 
Don't get me started about those stupid light bulbs.
 
subject: Is this method thread safe?