コードスニペット:
class Counter implements Runnable {
Object s = new Object();
@Override
public void run() {
try {
synchronized (s) {
s.wait(10000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
//...do Something
}
public void stopCounter() {
synchronized (s) {
s.notifyAll();
}
}
}
stopCounter を呼び出すかどうかに関係なく、...do Something コードは常に待機間隔の後にのみ実行されます。通知後も 10 秒間待機します。