How to ensure thread safety of utility static method?

前端 未结 6 529
南笙
南笙 2020-12-04 07:30

Is there any general way or rules exits by which we can ensure the thread safety of static methods specifically used in various Utility classes of any applications. Here I w

相关标签:
6条回答
  • 2020-12-04 07:58

    It is well known that static methods with immutable objects as parameters are thread safe and mutable objects are not.

    I would contest this. Arguments passed to a method are stored on a stack, which is a per-thread idiom.

    If your parameter is a mutable object such as a Date then you need to ensure other threads are not modifying it at the same time elsewhere. But that's a different matter unrelated to the thread-safety of your method.

    The method you posted is thread-safe. It maintains no state and operates only on its arguments.

    I would strongly recommend you read Java Concurrency in Practice, or a similar book dedicated to thread safety in Java. It's a complex subject that cannot be addressed appropriately through a few StackOverflow answers.

    0 讨论(0)
  • 2020-12-04 07:58

    Given the structure of the JVM, local variables, method parameters, and return values are inherently "thread-safe." But instance variables and class variables will only be thread-safe if you design your class appropriately. more here

    0 讨论(0)
  • 2020-12-04 08:11

    I would recommend creating a copy of that (mutable) object as soon as the method starts and use the copy instead of original parameter.

    Something like this

    public static Date getNormalizeDate(Date date) {
        Date input = new Date(date.getTime());
        // ...
    }
    
    0 讨论(0)
  • 2020-12-04 08:13

    Here's how I think of it: imagine a CampSite (that's a static method). As a camper, I can bring in a bunch of objects in my rucksack (that's arguments passed in on the stack). The CampSite provides me with a place to put my tent and my campstove, etc, but if the only thing the CampSite does is allow me to modify my own objects then it's threadsafe. The CampSite can even create things out of thin air (FirePit firepit = new FirePit();), which also get created on the stack.

    At any time I can disappear with all my objects in my ruckstack and one of any other campers can appear, doing exactly what they were doing the last time they disappeared. Different threads in this CampSite will not have access to objects on the stack created CampSite in other threads.

    Say there's only one campStove (a single object of CampStove, not separate instantiations). If by some stretch of the imagination I am sharing a CampStove object then there are multi-threading considerations. I don't want to turn on my campStove, disappear and then reappear after some other camper has turned it off - I would forever be checking if my hot dog was done and it never would be. You would have to put some synchronization somewhere... in the CampStove class, in the method that was calling the CampSite, or in the CampSite itself... but like Duncan Jones says, "that's a different matter".

    Note that even if we were camping in separate instantiations of non-static CampSite objects, sharing a campStove would have the same multi-threading considerations.

    0 讨论(0)
  • 2020-12-04 08:14

    Since your class does not hold any member variables, your method is stateless (it only uses local variables and the argument) and therefore is thread safe.

    The code that calls it might not be thread safe but that's another discussion. For example, Date not being thread safe, if the calling code reads a Date that has been written by another thread, you must use proper synchronization in the Date writing and reading code.

    0 讨论(0)
  • 2020-12-04 08:15

    I see a lot of answers but none really pointing out the reason.

    So this can be thought like this, Whenever a thread is created, it is created with its own stack (I guess the size of the stack at the time of creation is ~2MB). So any execution that happens actually happens within the context of this thread stack. Any variable that is created lives in the heap but it's reference lives in the stack with the exceptions being static variables which do not live in the thread stack.

    Any function call you make is actually pushed onto the thread stack, be it static or non-static. Since the complete method was pushed onto the stack, any variable creation that takes place lives within the stack (again exceptions being static variables) and only accessible to one thread.

    So all the methods are thread safe until they change the state of some static variable.

    0 讨论(0)
提交回复
热议问题