public void doDeposit ( int deposit ) { // Prevent two threads from updating the bank balance // at once. The fetch, addition, and store must be done // as one atomic unit, otherwise the bankBalance could get // out of whack. // Contention for this chuck of code // is resolved by locking the current object. // Any other critical code synchronized on this will also be locked // out when any thread is executing inside this critical region on this object. // I.e. there is one lock per object, not one per critical region. synchronized ( this ) { bankBalance += deposit; } } // When you want the whole routine synchronized on this // you can use the shorthand: public synchronized void doDeposit ( int deposit ) { bankBalance += deposit; }