# Concurrency - (Shared) Memory Inconsistency

Errors that result from inconsistent views of shared memory.

Memory consistency errors occur when different threads have inconsistent views of what should be the same data.

The results of a write by one thread are guaranteed to be visible to a read by another thread only if the write operation happens-before the read operation.

The key to avoiding memory consistency errors is understanding the happens-before relationship.

## 3 - Example

Suppose:

• A simple int field is defined and initialized:
int counter = 0;
• The counter field is shared between two threads, A and B.
counter++;
System.out.println(counter);