Synchronization

When several threads are using the same data, locking may be necessary to prevent unexpected results (like loss of updates).

Example:
    /* avoid balance from being modified
       while we update it */
    synchronized (balance) {
        balance += amount;
    }
Sometimes two threads need to explicitly tell each other when to continue. This is done through notification. Example:
    synchronized void doWhenCondition() {
        while (!condition)
        wait();
        // do whatever after condition is true
    }

    synchronized void changeCondition() {
        // change a condition
        notify();
    }