Results 1 to 7 of 7
Thread: Are static methods thread safe?
- 05-21-2010, 09:15 PM #1
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
Are static methods thread safe?
Hello,
I have created a utility class that has one static method. This method will be used by multiple threads. My static method has accepts two arguments and based on them it returns some value. The method is never dependent upon any member variables in FormUtils class. I am wondering is it thread safe?
Moderator Edit: Code tags addedJava Code:public class FormUtils { public static int compareLength(String strValue, String strLength) { // Set the initial length value int length = 0; try { length = Integer.parseInt(strLength); } catch (NumberFormatException e) { // Ignore - this will happen if the passed in value is null or empty } if (strValue == null) { if (length < 0) { return 1; } else if (length == 0) { return 0; } else { return -1; } } else { if (length < strValue.length()) { return 1; } else if (length == strValue.length()) { return 0; } else { return -1; } } } }Last edited by Fubarable; 05-21-2010 at 09:57 PM. Reason: Moderator Edit: Code tags added
- 05-21-2010, 09:21 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
yeah, it is, you're good.
- 05-21-2010, 09:44 PM #3
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
Even though I am passing two string arguments, which are not Java primitive types? If I had multiple threads going through that method concurrently? Would I achieve the result that I expected?
-
Hello, and welcome to the forum. I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.
To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Best of luck, and again, welcome!Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 05-21-2010, 11:52 PM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
when different threads will be calling your method, they will all pass their own version of the arguments. Think of it this way: each thread creates its own version of your method and runs that. No conflicts. The arguments are owned by the thread.
Now, if your method accessed some class variables, then all the "versions" of your method would be talking to the same objects - problem.
-
Cross-post: Java Programming - Is my static method thread safe?
Please read what JavaRanch says about cross-posting without being forthright about it: JavaRanch - Be Forthright When Cross Posting To Other Sites]
The same etiquette applies here and in the other programming fora.
- 06-04-2010, 02:17 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Are Local variables thread safe ?
By samson in forum Threads and SynchronizationReplies: 6Last Post: 12-21-2010, 02:34 PM -
Thread safe without using synchronization
By swetu.vc in forum Threads and SynchronizationReplies: 3Last Post: 01-20-2010, 08:06 AM -
Thread Safe Methods / Locks
By mrhyman in forum Threads and SynchronizationReplies: 16Last Post: 10-24-2008, 09:57 PM -
The safe way to stop a thread
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:31 PM -
Local Variables for a static method - thread safe?
By mikeg1z in forum Advanced JavaReplies: 1Last Post: 11-16-2007, 01:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks