Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure thread safety of utility static method?

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 want to specifically point out the thread safety of Web Applications.

It is well know that static methods with Immutable Objects as parameters are thread safe and Mutable Objects are not.

If I have a utility method for some manipulation of java.util.Date and that method accepts an instance of java.util.Date, then this method would not be thread safe. Then how to make it thread safe without changing the way of parameter passing?

public class DateUtils {      public static Date getNormalizeDate(Date date) {         // some operations     }    } 

Also is the class javax.faces.context.FacesContext mutable? Is it thread safe to pass an instance of this class to such static utility method?

This list of classes, instances of which can be or cannot be passed as parameters, could be long; so what points should we keep in mind while writing codes of such utility classes?

like image 946
Tapas Bose Avatar asked Dec 17 '12 08:12

Tapas Bose


People also ask

How do I make a static thread-safe?

Now if you want to make this method really thread safe then one simple thing you can do. Either use non-mutable variables / objects or do not change / modify any method parameters.

How do I make sure a method is thread-safe?

There is no rule that makes the code thread safe, the only thing you can do is make sure that your code will work no matter how many times is it being actively executed, each thread can be interrupted at any point, with each thread being in its own state/location, and this for each function (static or otherwise) that ...

Is static function thread-safe?

Static functions are no more and no less safe than non-static ones. Being static and being thread-safe are completely orthogonal. So that said Singleton, one of the popular design pattern, is not recommended.

Can static methods be protected?

Actually there is nothing fundamentally wrong with protected static . If you really want a static variable or method that is visible for the package and all subclasses of the declaring class then go ahead and make it protected static .


2 Answers

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.

like image 172
Duncan Jones Avatar answered Oct 06 '22 00:10

Duncan Jones


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.

like image 28
assylias Avatar answered Oct 06 '22 01:10

assylias