Reputation: 9487
What are some situations where you need to worry about whether a static method is thread safe?
For example, if I have static utility function that doesn't touch any static class level variables, is that method already thread safe? If I have a static method that does touch static class variables, is that method potentially not thread safe?
Thanks in advance.
Upvotes: 5
Views: 2341
Reputation: 837926
If I have static utility function that doesn't touch any static class level variables, is that method already thread safe?
Most of the time - what matters is if your method is reentrant. Something like this is reentrant because everything is local variables and each thread receives its own copy:
static int add(int a, int b)
{
return a + b;
}
There are still some things to be careful of. If you pass in an object to the method and the method mutates the object and before the method completes you call the same method again on a different thread but with the same object, then you have a problem.
If I have a static method that does touch static class variables, is that method potentially not thread safe?
The main issue again is if the variables are mutable. If you are only reading from immutable objects then there may not be any problem.
If you call a method and you are not sure if it is reentrant and the documentation doesn't say, it's best to assume that it isn't.
Upvotes: 6
Reputation: 7038
If you using only local stack variables in static method then there is no reason to worry. java.lang.Math.min(int, int) is good example of such method. But in case if you touching any shared static variables, or objects which do have state (aka not immutable) then you have to use synchronization.
Upvotes: 5
Reputation: 60006
If you have a method that writes to any state that's shared across multiple threads (for example, static variables, or any objects that multiple threads use), then that method needs to worry about thread-safety, and so do any methods that read any of that shared state.
Upvotes: 2
Reputation: 21618
You have to worry about thread safety whenever you have more than one thread accessing the same resources, and those resources are not immutable.
Upvotes: 2
Reputation: 26703
Thread safety becomes a concern if there is at least a single entry point which can be accessed by multiple threads.
If a piece of code is accessed by multiple threads and is calling other method/class/etc., then all this code tree becomes vulnerable.
From "Java concurrency in practice": The first step in organizing a program around task execution is identifying sensible task boundaries. Ideally, tasks are independent activities: work that doesn’t depend on the state, result, or side effects of other tasks. Independence facilitates concurrency, as independent tasks can be executed in parallel if there are adequate processing resources.
For example, if you are writing an isolated servlet, which is guaranteed to be executed by a single thread, you don't have to worry. But if you write a static utility which is used by different servlets, then it has to be made to handle multi-threaded access.
Upvotes: 0
Reputation: 17412
It is thread-safe if no mutable resources, such as, class variables are accessed -- however, you have to make sure that your method does not call any other methods that access any mutable resources.
Upvotes: 2
Reputation: 86381
You have to worry about thread safety whenever you have more than one thread accessing the same resources.
Your static method might be unsafe if it is using the same resources as other threads -- directly or indirectly -- even if those resources are not static class variables.
Upvotes: 3