Explain Codes LogoExplain Codes Logo

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

java
thread-safety
static-methods
concurrency
Alex KataevbyAlex Kataev·Jan 29, 2025
TLDR
**Thread safety in static methods** is assured **only if** they are **pure functions**, without interaction with shared mutable state. Check: - No access or modifications to **static variables**. - No alterations to **input parameters**. - No reliance on **outer mutable state**. Here's an example: ```java public static int sum(int a, int b) { return a + b; // Purely local, like your favorite small coffee shop. No changes occur outside. }

The sum method is thread-safe; it calculates using its parameters, creating deliciously simple functions without any side effects.

Thread safety and local variables

Local variables are stored in the stack, making them isolated to each thread. Picture each thread having its own stack, walled-off from the rest; local variables are safely confined.

Immutable objects are essential contributors to thread safety. They're like well-cooked pasta: done just once, and it's ready forever. Instances such as String objects are immutable, and employing them won't risk any thread safety.

The game of visibility and atomicity

The dimensions of atomicity and visibility play a pivotal role when sharing data between threads.

  • Atomicity: Operations are all or nothing—like a game of Jenga, you can't leave it halfway.
  • Visibility: Changes made by one thread to shared data are visible to all—like a reality TV show where everyone sees everyone's drama.

volatile guarantess visibility, while Atomic classes in Java protect atomicity.

Patterns of synchronization: A deeper dig

Synchronization isn't the lone ranger of thread safety. Effective thread-safe patterns include:

  • The use of immutable objects
  • Thread confinement practices
  • Thread-safe collections like ConcurrentHashMap
  • Higher-level concurrency controls like Lock and Semaphore

However, remember that cooking with premium ingredients doesn't imply a Michelin star meal—misuse of patterns such as ConcurrentHashMap might still crash your thread safety party.

Cut the shared mutable state

You can achieve thread safety by designing static methods that avoid shared mutable state:

  • Methods neither read nor modify mutable instance or class variables.
  • State is passed via method parameters or is enclosed within local variables.
  • Returning new objects or deep copies to maintain encapsulation and avoid unwelcome external changes.

Even without synchronization, these designs make static methods thread-safe.

Understanding how thread safety operates with Java collections and APIs is critical. The jungle of standard collections isn't universally thread-safe, and even thread-safe collections require additional synchronization when iterating.

JDK API functions may be unsynchronised; always verify their thread safety before diving in.

Remember to use safe publication techniques for secure initialization of complex objects shared among threads.