Re: Old famous problem of killing a thread

Originally Posted by
guyafe
I want the implementer that writes iterate() and any other method that uses getMessage() wont be able to catch this exception and handle it by himself.
Can I do that?
No, I don't think so. If the subclass calls a method that throws an exception, it can catch that exception.
Why don't you do it the traditional way with a private 'finished' boolean member variable in the superclass that you set true from getMessage and check in the while loop in the run method?
Code:
abstract class AbstractAgent implements Runnable {
private MailBox m_mailbox;
private boolean finished = false;
protected final AbstractMessage getMessage() {
AbstractMessage message = this.m_mailbox.getMessage();
if (message.getType == KILL_MESSAGE) {
finished = true;
}
else {
return message;
}
}
protected abstract void iterate();
public void run () {
while(!finished) {
this.iterate();
}
}
}
The unavoidable price of reliability is simplicity...
C.A.R. Hoare
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.