Home > Java > javaTutorial > Atomic, Volatile, or Synchronized: Which Approach Guarantees Thread Safety?

Atomic, Volatile, or Synchronized: Which Approach Guarantees Thread Safety?

Linda Hamilton
Release: 2024-12-08 03:01:10
Original
941 people have browsed it

Atomic, Volatile, or Synchronized: Which Approach Guarantees Thread Safety?

The Differences Between Atomic, Volatile, and Synchronized

This discussion explores the internal workings of atomic, volatile, and synchronized constructs in programming.

Non-Synchronized Increment

private int counter;

public int getNextUniqueIndex() {
    return counter++; 
}
Copy after login

This straightforward approach experiences concurrency issues in multi-threaded environments due to race conditions and memory visibility problems. Each thread may have its own local copy of the counter, leading to data inconsistencies.

AtomicInteger

private AtomicInteger counter = new AtomicInteger();

public int getNextUniqueIndex() {
    return counter.getAndIncrement();
}
Copy after login

AtomicInteger utilizes CAS (compare-and-swap) operations to ensure thread-safety. It reads the current value of the counter, increments it, and atomically compares and swaps the new value with the previous one.

Volatile without Synchronization

private volatile int counter;

public int getNextUniqueIndex() {
    return counter++; 
}
Copy after login

This approach only addresses the memory visibility issue but not the race condition. The pre/post-increment operation remains non-atomic.

Volatile without Synchronization (i = 5)

volatile int i = 0;
void incIBy5() {
    i += 5;
}
Copy after login

This code illustrates the limited utility of volatile. Even though it ensures visibility, the underlying operation is not atomic, resulting in race conditions.

Synchronized Blocks

void incIBy5() {
    int temp;
    synchronized(i) { temp = i }
    synchronized(i) { i = temp + 5 }
}
Copy after login

This attempt at synchronization is flawed because the lock object changes with each execution, rendering the synchronized blocks ineffective. The locks must be consistent across the entire operation to ensure thread safety.

To conclude, atomic constructs like AtomicInteger provide thread-safe operations without the need for synchronized blocks. Volatile ensures memory visibility but does not guarantee atomicity. Synchronized blocks, when used correctly, offer explicit control over thread access to shared resources.

The above is the detailed content of Atomic, Volatile, or Synchronized: Which Approach Guarantees Thread Safety?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template