Suppose that a token machine can only issue one token at a time. The rule is that one token must be consumed before another one can be created.
class TokenMachine {
private int tokenID;
private boolean tokenExists = false;
public synchronized void createToken(int coup){
...
}
public synchronized int consumeToken(){
...
}
}
public synchronized void createToken(int coup) {
while(tokenExists) { //can I change 'while' to 'if'?
wait(); //in a try-catch block
}
this.tokenID = coup;
tokenExists = true;
notify();
}
public synchronized int consumeToken() {
while(!tokenExists) { //can I change 'while' to 'if'?
wait(); //in a try-catch block
}
tokenExists = false;
notify();
return tokenID;
}
My question is that can I change the 'while' expression in previous code to 'if' instead without vandalize the rule? Many thanks.
Thanks guys for answering my question, I checked a lot online, find the following info are helpful: A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops.
Apparently, the spurious wakeup is an issue (I doubt that it is a well known issue) that intermediate to expert developers know it can happen but it just has been clarified in JLS third edition which has been revised as part of JDK 5 development. The javadoc of wait method in JDK 5 has also been updated