Runnable: run method runs
in new thread
Runnable r = new MyRunnable(); Thread t = new Thread(r); t.start();
Thread.sleep(millis) if they have nothing
to doInterruptedException. Catch it in the outermost level
of run:
public class MyRunnable implements Runnable {
public void run() {
try {
while (...) {
do work
Thread.sleep(...);
}
} catch (InterruptedException e) {}
clean up
}
}


public class BankAccount {
. . .
private Lock balanceChangeLock = new ReentrantLock();
}
public void deposit(double amount) {
balanceChangeLock.lock();
try {
access shared resources
} finally {
balanceChangeLock.unlock();
}
}
balanceChangeLock.lock();
try {
while (balance < amount) // cannot withdraw
// wait
. . .
public class BankAccount {
. . .
private Condition sufficientFundsCondition = balanceChangeLock.newCondition();
}
await when thread can't proceed
while (balance < amount) sufficientFundsCondition.await();
signalAll
balance += amount; sufficientFundsCondition.signalAll();
public monitor BankAccount {
double balance; // automatically private
public void deposit() { ... } // automatically acquires lock
synchronized to acquire/release the intrinsic lock
public synchronized void deposit(double amount) {
balance += amount;
notifyAll();
}
wait and notifyAll methods in
Object (!) operate on intrinsic condition
public synchronized void withdraw(double amount) throws InterruptedException {
while (balance < amount) wait();
balance -= amount;
}
synchronized (anObj) { . . . }