Synkronisering koordinerer trådenes adgang til delt mutable tilstand, så operationer ikke interleaves farligt og forebygger race conditions. Java tilbyder flere mekanismer, fra det indbyggede synchronized nøgleord til eksplicitte locks og lock-free atomics.
synchronized — mutual exclusion (intrinsic locks)
{
balance;
{
(balance >= amount) {
balance -= amount;
}
}
{
() {
balance += amount;
}
}
}
