Results 1 to 7 of 7
  1. #1
    sinisa.medic is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default 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?

    Java 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;
                }   
            }
        }
    
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 05-21-2010 at 09:57 PM. Reason: Moderator Edit: Code tags added

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    yeah, it is, you're good.

  3. #3
    sinisa.medic is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default

    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?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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 [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck, and again, welcome!

  5. #5
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default

    The spammer is gone so my reply is out of context now (edited)

    kind regards,

    Jos
    Last edited by JosAH; 06-05-2010 at 02:11 PM.

Similar Threads

  1. Are Local variables thread safe ?
    By samson in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-21-2010, 02:34 PM
  2. Thread safe without using synchronization
    By swetu.vc in forum Threads and Synchronization
    Replies: 3
    Last Post: 01-20-2010, 08:06 AM
  3. Thread Safe Methods / Locks
    By mrhyman in forum Threads and Synchronization
    Replies: 16
    Last Post: 10-24-2008, 09:57 PM
  4. The safe way to stop a thread
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:31 PM
  5. Local Variables for a static method - thread safe?
    By mikeg1z in forum Advanced Java
    Replies: 1
    Last Post: 11-16-2007, 01:06 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •