Synchronization coordinates threads' access to shared mutable state so that operations don't interleave dangerously, preventing race conditions. Java offers several mechanisms, from the built-in synchronized keyword to explicit locks and lock-free atomics.
synchronized — mutual exclusion (intrinsic locks)
{
balance;
{
(balance >= amount) {
balance -= amount;
}
}
{
() {
balance += amount;
}
}
}
