When several threads are using the same data, locking may be necessary to prevent unexpected results (like loss of updates).
/* 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.
synchronized void doWhenCondition() { while (!condition) wait(); // do whatever after condition is true } synchronized void changeCondition() { // change a condition notify(); }