I want to implement the usual cooperative mechanism for canceling a thread. However the java memory model was only fixed in JDK5 while I'm in a pre-JDK5 environment. I understand, this means that doing something like this, as espoused in SCIP, will not be correct.
class Worker implements Runnable
{
private volatile boolean _canceled;
public void cancel() { _canceled = true; }
public void run() {
while( ! _canceled )
{
// do my shit
}
}
}
I'm thinking of using an AtomicBoolean to encapsulate the _canceled variable. Any other alternatives ? Thanks Stackoverflow.