Are non-synchronised static methods thread safe if they don’t modify static class variables?

This method is 100% thread safe, it would be even if it wasn’t static. The problem with thread-safety arises when you need to share data between threads – you must take care of atomicity, visibility, etc.

This method only operates on parameters, which reside on stack and references to immutable objects on heap. Stack is inherently local to the thread, so no sharing of data occurs, ever.

Immutable objects (String in this case) are also thread-safe because once created they can’t be changed and all threads see the same value. On the other hand if the method was accepting (mutable) Date you could have had a problem. Two threads can simultaneously modify that same object instance, causing race conditions and visibility problems.

Leave a Comment