# Concurrency - Thread Interference (Interleave on shared data)

how errors are introduced when multiple threads access shared data.

Interference happens when two operations, running in different threads, but acting on the same data, interleave. This means that the two operations consist of multiple steps, and the sequences of steps overlap.

Resolved by Concurrency - Synchronization

Because they are unpredictable, thread interference bugs can be difficult to detect and fix.

Same name than ? Concurrency - Race Condition

## 3 - Interleave Counter Example

• A counter c with two single expression increment and decrement.
public void increment() {
c++;  // c-- for decrement
}
• This single expression translate to multiple steps by the virtual machine
Retrieve the current value of c.
Increment the retrieved value by 1.
Store the incremented value back in c.
• Example of a particular interleaving where Thread A data operation is lost
Thread A: Retrieve c.
Thread A's result is lost, overwritten by Thread B.